0

I have a vb.net solution with a separate project containing a file class to access Azure files and 2 projects. In both I call the file class which contains this code

Dim mascThis as ShareClient
... mascThis is initialized ...
If mascThis.Exists.Value then ... do something ..

In one project this works, in the other I get the message

Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I've seen this problem discussed before (assembly issues), but all projects are in the same solution in to which I added Azure.Storage.Files.Shares 12.8.0 via Nuget and only the "File class" project has a reference to it, the other projects don't.

So what am I missing here?

1 Answers1

0

Please check if any of the below is your case.

Sometimes, we will have the situation where different parts(projects) of our solution depend on different versions of the same DLL i.e; assemblies with the same assembly name.

The error

Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

suggests , your project is looking for assembly version 4.0.4.1

Work arounds

i. Usually Nuget Package 4.5.3 contains assembly version 4.0.4.1. Please check if both the projects in your solution can be worked on that assembly version and add that particular version as new version(ex:4.0.4.1) through binding redirect in the project where you are getting error. And keep the old version in place of old version.

Also Right click on the package reference and set 'specific version' to false under its properties

Example:

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
          </dependentAssembly>

In this example ,this way of adding specifies that the runtime should use version 4.0.6.0 in the Assembly versions between olderversion range 0.0.0.0-4.0.6.0

Else If above is not the case, the solution might need different versions.

ii.

  1. Right click in the project properties and choose the ApplicationConfiguration file and App.config

  2. Add the following settings in the app.config file.

To configure multiple assemblies with the same name through codeBases.

Sample code for some package say “A” :

<configuration>
 <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="A " publicKeyToken="3d67ed1f87d44c89" />
        <codeBase version="3.0" href="...\A.dll"/>
        <codeBase version="5.0" href="...\A.dll"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

iii. See if you could solve that problem by using extern alias.

iv. Binding redirects are added if your app or its components reference more than one version of the same assembly .See Enable or disable autogenerated binding redirects | Microsoft Docs .If you do it manually ,you need to disable under project properties. Manually you can add <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> in csproj file in property group. Ex:

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

References that can be helpful.

kavyaS
  • 8,026
  • 1
  • 7
  • 19
  • Thanks for the overview of possibilities, this helped in approaching the problem systematically. – Ben Gijsen Oct 24 '21 at 09:19
  • Thanks for the overview of possibilities, this helped in approaching the problem systematically. "Binding redirection" didn't work (even when I unchecked "Auto-generate binding redirects" for the project. Can this be because of indirect references or some form of "hard" coding of a reference in one of the dll's? Using "codebase" with a locally made copy of version 4.0.4.1 worked when I ran within Visual Studio, but not after I created a .msi where I did add the local copy as an "assembly" to the file and included the app.config file. What could be the reason for that? – Ben Gijsen Oct 24 '21 at 09:31