0

我一直在尝试从微软的 odata 客户端运行 NAV 功能(Microsoft Dynamics 365 Business Central)。当我创建一个 odatav4 数据源并导入$metadata时,我能够在自动生成的代码中包含 codeunits 函数,这为我创建了一个 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();

邮递员示例(最后您可以看到 url 参数:?company=mycompany邮递员样品

4

1 回答 1

2

插入公司

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

示例代码

在生成的 NAV 类中,我添加了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();
于 2021-12-09T09:00:33.523 回答