Search code examples
asp.netapiazure-active-directorydynamics-crmcrm

Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.19.8.16603. does not match the assembly reference


There are two projects in solution.One is API and other is for OrganizationServiceCall.

I have installed one nuget package called Microsoft.CrmSdk.XrmTooling.CoreAssembly it has installed default package Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.19.8.1660 with it.

But I have installed Microsoft.IdentityModel.Clients.ActiveDirectory, Version=2.22 in other project.While Call API I got this error :

{"Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.19.8.16603"}.

How can i done work while same Solution have different version of nuget pacakge conflicts?


Solution

  • Some work arounds that you can try

    1. If second project api has no issue with latest version ,Also try Upgrading it to Microsoft.IdentityModel.Clients.ActiveDirectory version 3.19.8

    2. Or Update all NuGet packages to the latest if its ok.

    3. Or one may need to edit your csproj manually to add that specific version in thart particular project.

    4. Use a single assembly version with Binding Redirect in config file

      i) NuGet won't handle the references in the program. It just manages packages. We may have to force our project to use the loaded assembly with binding redirect.

      ii) This specifies which version of assembly to use instead of old version. It is not necessarily requires later version be specified in newVersion, earlier version can be provided as well in newVersion. If there are different versions, try make them uniform across projects. Issue should be solved.

      Here oneproject is referred to ActiveDirectory versions > 3.0, other project needs less version that that. Adding a binding redirect to the app.config can help fix problem in some cases. But before that please make sure that particular dlls are present in the configuration file.

      Note that the runtime section is the one to be added.

      <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
          <startup> 
           ...
        <runtime>
                ....
            <dependentAssembly>       
              <assemblyIdentity 
       name="Microsoft.IdentityModel.Clients.ActiveDirectory"  publicKeyToken="31bf3856ad364e35" culture="neutral" />
              <bindingRedirect oldVersion="3.0.0.0-3.19.8.16603" newVersion="3.19.8.16603" />
            </dependentAssembly>
           .....
        </runtime>
      </configuration>
      

    And try to explicitly set Specific Version for whatever DLL giving you version issues(Microsoft.IdentityModel.Clients.ActiveDirectory) to False from Visual Studio. enter image description here

    Other way, you can try is to auto-generate binding redirects.

    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
     <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    

    References:

    1. azure-sdk-for-media-services :issues(github)
    2. How to resolve “Could not load file or assembly | by Chirag Rupani | Medium
    3. Troubleshooting NuGet Package Restore in Visual Studio | Microsoft Docs