代码之家  ›  专栏  ›  技术社区  ›  Marcus Aurelio

使用Azure Functions 2.0连接到Dynamics 365时出现错误:“无法加载指定的文件”

  •  0
  • Marcus Aurelio  · 技术社区  · 6 年前

    我正在尝试使用Azure功能连接到Dynamics 365 Online。我关注了一个在线教程,但这些教程是基于Azure函数1的。

    我收到的错误是:

    2018-11-03T05:21:00.621 [Information] Executing 'Functions.Test' (Reason='This function was programmatically called via the host APIs.', Id=c6ddb58b-51dc-4679-b985-4bcc7934c246)
    2018-11-03T05:21:00.984 [Error] Executed 'Functions.Test' (Failed, Id=c6ddb58b-51dc-4679-b985-4bcc7934c246)
    Could not load the specified file.
    

    我的代码如下:

    #r "Newtonsoft.Json"
    using System.Net;
    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using System.ServiceModel;
    using Microsoft.Xrm.Tooling.Connector;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Primitives;
    //using Newtonsoft.Json;
    
    public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
    {
       // log.LogInformation("C# HTTP trigger function processed a request.");
    
        string name = req.Query["name"];
    
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
       // dynamic data = JsonConvert.DeserializeObject(requestBody);
        //name = name ?? data?.name;
    
       // log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    
                    var connectionString = @"AuthType = Office365;
                    Url = https://*******.crm4.dynamics.com/;Username=******@*****;Password=****";
                    CrmServiceClient conn = new CrmServiceClient(connectionString);
                                    IOrganizationService service;
                    service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
    
                       Entity lead = new Entity("lead");
                        lead.Attributes["subject"] = "New Lead: " + name ;
                        service.Create(lead);
    
    
    
    
        return name != null
            ? (ActionResult)new OkObjectResult($"Hello, {name}")
            : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
    }
    

    我的函数.proj是:

    <Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.CrmSdk.CoreAssemblies" Version="9.0.2.5"/>
        <PackageReference Include="System.ServiceModel.Security" Version="4.5.3"/>
        <PackageReference Include="Microsoft.CrmSdk.XrmTooling.CoreAssembly" Version="9.0.2.7"/>
      </ItemGroup>
    
    </Project>
    

    1 回复  |  直到 6 年前
        1
  •  0
  •   Jerry Liu Phantom    6 年前

    加载程序集时出现问题 Microsoft.CrmSdk.XrmTooling.CoreAssembly 包的目标是.Net Framework,而函数的目标是.Net Standard。

    如果没有.net核心sdk,我们可能不得不放弃CrmSdk并使用Azure Active Directory进行身份验证。请注意,通过这种方式,您必须拥有具有系统管理员安全角色和Office 365订阅的全局管理员角色的Dynamics 365(联机)用户帐户。

    1. steps

    2. 那么 authenticate using ADAL .

    3. Xrm.Tools.CRMWebAPI ,它是用于使用公共数据服务(CDS)和Dynamics 365 Web API的API助手。