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

时间:2018-11-03 05:26:01

标签: c# azure dynamics-crm azure-functions microsoft-dynamics

我正在尝试使用Azure Functions连接到Dynamics 365 Online。我已经在线上阅读了一个教程,但是这些教程是基于Azure Functions 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");
}

我的functions.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 个答案:

答案 0 :(得分:0)

将程序集作为Microsoft.CrmSdk.XrmTooling.CoreAssembly包目标以.Net Framework加载而函数以.NET Standard为目标。

更新

我做错了,手动将这些程序集添加到上下文中不起作用。

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

  1. 向Azure Active Directory注册Dynamic 365应用。遵循这些steps

  2. 然后authenticate using ADAL

  3. 选中此nuget软件包Xrm.Tools.CRMWebAPI,它是用于使用Common Data Service(CDS)和Dynamics 365 Web API的API帮助程序。