c# OdataClient:DataServiceActionQuery,如何执行 Microsoft Dynamics 365 Business Central 的功能?

c# OdataClient: DataServiceActionQuery, how to execute a function Microsoft Dynamics 365 Business Central?

我一直在尝试 运行 来自 Microsoft 的 odata 客户端的 NAV 功能 (Microsoft Dynamics 365 Business Central)。当我创建一个 odatav4 数据源并导入 $metadata 时,我能够在自动生成的代码中包含代码单元函数,这为我创建了一个 DataServiceActionQuery 类型:

 /// <summary>
 /// There are no comments for Funcions_TestFunction in the schema.
 /// </summary>
 [global::Microsoft.OData.Client.OriginalNameAttribute("Funcions_TestFunction")]
 public virtual global::Microsoft.OData.Client.DataServiceActionQuery Funcions_TestFunction(string param1, string param2, string param3)
    {
        return new global::Microsoft.OData.Client.DataServiceActionQuery(this, this.BaseUri.OriginalString.Trim('/') + "/Funcions_TestFunction", new global::Microsoft.OData.Client.BodyOperationParameter("param1", param1),
                new global::Microsoft.OData.Client.BodyOperationParameter("param2", param2),
                new global::Microsoft.OData.Client.BodyOperationParameter("param3", param3));
    }

当我尝试运行它时,我必须将公司代码传递给它,但我不知道该怎么做:

 Uri texte = new Uri(@"http://192.168.0.18:18148/Instancename/ODataV4/");
 NAV.NAV x = new NAV.NAV(texte);
 x.Credentials = credentials;
 DataServiceActionQuery funcionini = x.Funcions_TestFunction(param1, param2, param3);
 funcionini.Execute();

使用 DataServiceQuery(已发布页面)我没有问题可以传递它,只需添加查询选项:

 DataServiceQuery<NAV.Customer> z = x.Customer.AddQueryOption("company", "Mycompany");
 IEnumerable<NAV.Customer> resultat = z.Where(f => f.Type == "something").OrderBy(f => f.Name);//.GetAllPages();

sample with postman(最后可以看到url参数:?company=mycompany

插入公司

  1. 最简单的方法:将"MyCompany"公司放在URL:
Uri texte = new Uri(@"http://192.168.0.18:18148/Instancename/ODataV4/Company(MyCompany)");
  1. 更优雅:将公司 header 添加到每个 NAV 请求中。我更喜欢 EventHandler BuildingRequest 来做到这一点:
NAV.BuildingRequest += (_, e) => e.Headers.Add("company", "MyCompany");

示例代码

在生成的 NAV class 中,我添加了 CredentialsBuildingRequest。我还添加了 SendingRequest2ReceivingResponse 用于调试:

public NAV(global::System.Uri serviceRoot) : base(serviceRoot, global::Microsoft.OData.Client.ODataProtocolVersion.V4)
{
    // generated Code in constructor
    this.ResolveName = new global::System.Func<global::System.Type, string>(this.ResolveNameFromType);
    this.ResolveType = new global::System.Func<string, global::System.Type>(this.ResolveTypeFromName);
    this.OnContextCreated();
    this.Format.LoadServiceModel = GeneratedEdmModel.GetInstance;
    this.Format.UseJson();

    // my additions
    Credentials = new System.Net.NetworkCredential("user", "pw", "domain");
    BuildingRequest += OnBuild;
    SendingRequest2 += showRequest;     // for debugging
    ReceivingResponse += showResponse;  // for debugging
}

private void OnBuild(object _, BuildingRequestEventArgs e)
{
    e.Headers.Add("company", "myCompany");
}

private void showResponse(object sender, ReceivingResponseEventArgs e)
{
    // for debugging
}

private void showRequest(object sender, SendingRequest2EventArgs e)
{
    // for debugging
}

那么你应该可以调用你的函数了:

var x = new NAV.NAV(new Uri("uri"));
DataServiceActionQuery funcionini = x.Funcions_TestFunction(param1, param2, param3);
funcionini.Execute();

我的代码生成工具生成了 DataServiceActionQuerySingle 而不是 DataServiceActionQuery。我猜测 这取决于函数在 NAV 中的定义方式。我这样调用函数:

var x = new NAV.NAV(new Uri("uri"));
string result = x.FunctionReturningString("test").GetValue();