Azure-sdk-for-net: Unable to have Microsoft.CrmSdk.XrmTooling.CoreAssembly and Microsoft.Azure.KeyVault in same project

Created on 1 Oct 2019  ·  13Comments  ·  Source: Azure/azure-sdk-for-net

Query/Question
I've added Microsoft.CrmSdk.XrmTooling.CoreAssembly to my project so that I can connect to a Dynamics 365 instance using new CrmServiceClient(connectionString) rather than using OranisationServiceProxy as CrmServiceClient is meant to work better for long running data replication jobs.

However, I now get this exception

System.IO.FileLoadException: Could not load file or assembly 'Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Any suggestions?

I have tried adding binding redirect but does nothing
app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Rest.ClientRuntime" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.3.0.0" newVersion="2.3.20.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Why is this not a Bug or a feature Request?
Because it was working until added new dependency

Setup (please complete the following information if applicable):

  • OS: Windows 10
  • IDE : Visual Studio
  • Version of the Library used: 2.3.20

myapp.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net462</TargetFramework>
    <RootNamespace>WNZL.Azure.Common</RootNamespace>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AdaptiveCards" Version="1.2.3" />
    <PackageReference Include="Microsoft.Azure.KeyVault" Version="3.0.4" />
    <PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.3.1" />
    <PackageReference Include="Microsoft.CrmSdk.CoreAssemblies" Version="9.0.2.17" />
    <PackageReference Include="Microsoft.CrmSdk.XrmTooling.CoreAssembly" Version="9.1.0.13" />
    <PackageReference Include="Microsoft.Graph" Version="1.17.0" />
    <PackageReference Include="Microsoft.Identity.Client" Version="4.4.0" />
    <PackageReference Include="Microsoft.Rest.ClientRuntime" Version="2.3.20" />
  </ItemGroup>
</Project>

Information Checklist
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

  • [Y] Query Added
  • [Y] Setup information Added
Client KeyVault

All 13 comments

Assuming KeyVault is definitely the conflicting dll:
@schaabs Can we provide a work-around here? .NET assembly bindings are not my area of expertise. Also, we should probably speak with Dynamics about how to fix this in the future.
Also cc: @weshaggard from EngSys. Wes - have we worked with Dynamics at all on dep conflict resolutions?

@andrew-summer do you have the fusion logs for this? Is this a web app or a desktop app?

The assembly version is still 2.0.0.0. Only the Microsoft.Rest.ClientRuntime package version is 2.3.20. What we really need to see are the fusion logs for the error, which you can get using fuslogvw.exe that is part of the .NET Framework SDK. See https://docs.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer for help finding and using it.

Without sample code, I'm also unable to repro this problem. Using a couple of types both from Key Vault and CRM packages but with the same package references shown above, I don't get a runtime binding error; however, your fusion logs would be faster for you to produce and for us to diagnose, than trying to distill a simple code sample that demonstrates this.

I had reverted to old method, but will re-implement and get results to you

I couldn't get anything out of the fuslogvw, however I did narrow the issue down to adding nuget package Microsoft.Azure.Services.AppAuthentication causing

System.IO.FileLoadException: Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.19.8.16603, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. 
The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I have a small project to demo I can supply if this if useful.

Current nuget references are:

<ItemGroup>
    <PackageReference Include="Microsoft.Azure.KeyVault">
      <Version>3.0.4</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.Azure.Services.AppAuthentication">
      <Version>1.3.1</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.CrmSdk.XrmTooling.CoreAssembly">
      <Version>9.1.0.21</Version>
    </PackageReference>
</ItemGroup>

Steps:

  1. Using just Microsoft.CrmSdk.XrmTooling.CoreAssembly was able to connect to CRM.
  2. After adding additional libraries for KeyVault got the above issue

Do you have a small sample snippet of code that can reproduce the results (eliding any private details, like an actual connection string)? In general, adding packages along shouldn't cause the issue unless you actually reference types from those libraries. I tried writing a small sample that used types from both assemblies, but apparently never ran the code path which causes this issue. To debug the issue, I need to be able to reproduce it.

Here’s a link to an example project on GitHub: https://github.com/andrew-sumner/CrmSdkIssue

The solution has two project (plus test projects):

  1. CrmSdkIssue: uses new CrmServiceClient and Microsoft.CrmSdk.XrmTooling.CoreAssembly:9.1.0.21
  2. KeyVaultTest: Have to use Microsoft.Azure.KeyVault:2.3.2 otherwise cannot load Newtonsoft.json (breaks all tests)
  3. GraphTest: ok
  4. CRMTest: Fails with “Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory” – if remove KeyVault and Graph packages it works fine

  5. CrmSdkWorking: uses old OrganizationServiceProxy connection and Microsoft.CrmSdk.CoreAssemblies:9.0.2.19

  6. KeyVaultTest: Have to use Microsoft.Azure.KeyVault:2.3.2 otherwise cannot load Newtonsoft.json (breaks keyvault and graph tests)
  7. GraphTest: ok
  8. CRMTest: ok

Have you had a chance to look at my example project?

Sorry, I have not. We've been very busy on the new libraries that will replace these and have far fewer dependencies (to help avoid problems like this), but will still support these older ones for some time. I'm close to being able to get back to this one.

Great thanks

I found that by upgrading to Microsoft.CrmSdk.XrmTooling.CoreAssembly v9.1.0.21 this problem goes away. (NOTE: it also changed the hint path in the csproj file to point to the Microsoft.Rest.ClientRuntine.dll that was provided by the XrmTooling)

That may indeed be the only problem. Even if I were to update the dependency package version, the diamond dependency problem won't go away. Often times, the solution is to update the dependencies causing problems to their latest version - most often within their same major version (if following semver guidance properly).

This is one big reason that the newer "track 2" libraries - Azure.* - take few dependencies, especially on things like MSAL that often introduces breaking changes across major versions. We recommend that people upgrade to Azure.* packages for this and other benefits, including faster pipelines, fewer allocations, and a more extensible pipeline (custom logging, tracing, policies, and more).

@andrew-sumner, I submitted a PR as more of an example of how you can upgrade your package references to resolve not only the original issue, but can upgrade Key Vault packages to the latest track 1 (Microsoft.Azure.) versions. This resolves the diamond dependency problem. Even if we upgraded our dependency versions, this problem would exist which is why we significantly reduced our dependencies in track 2 libraryes (Azure.) to, most often, little more than netstandard2.0.

https://github.com/andrew-sumner/CrmSdkIssue/pull/1

Please reopen with more detail if this does resolve your issue with your real-world project and you are unable to resolve the dependency problem through package upgrades and binding redirects in app.config.

Was this page helpful?
0 / 5 - 0 ratings