Azure function error: Could not load file or assem

2019-08-21 20:28发布

问题:

I m doing a azure function and testing it locally where it will add some data to Dyanmcis CRM. When I run function it throw this error:

Could not load file or assembly 'Microsoft.Xrm.Sdk, Version=7.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

Please note my code uses Microsoft.Xrm.Client(version 7) and it has Microsoft.Xrm.Sdk (version 8.1.0.235).

Please help.

回答1:

The problem is due to the missing assembly redirect. You can see exactly which assembly redirect is needed by adding the Microsoft.CrmSdk.CoreAssemblies NuGet package to a normal .Net project and looking at the generated binding redirect statements added to app.config.

Currently Azure Functions do not support setting assembly redirects, so it has to be done in code. I solved this problem by using the code from this blog post: How to fix the assembly binding redirect problem in Azure Functions

With that code, local.settings.json looks like this:

"Values": {
    "AzureWebJobsStorage": "",
    "AzureWebJobsDashboard": "",
    "BindingRedirects": "[ { \"ShortName\": \"Microsoft.Xrm.Sdk\", \"RedirectToVersion\": \"8.0.0.0\", \"PublicKeyToken\": \"31bf3856ad364e35\" } ]"
}

and in Application settings in the Azure portal:

[ { "ShortName": "Microsoft.Xrm.Sdk", "RedirectToVersion": "8.0.0.0", "PublicKeyToken": "31bf3856ad364e35" } ]