Could Not Load File Or Assembly System.Runtime.CompilerServices.Unsafe


Answer :


Could not load file or assembly System.Runtime.CompilerServices.Unsafe



It seems that you have installed System.Runtime.CompilerServices.Unsafe nuget package 4.5.3 version. And it corresponds to System.Runtime.CompilerServices.Unsafe.dll assembly version 4.0.4.1.


Suggestion


1) Please try to register System.Runtime.CompilerServices.Unsafe version 4.0.4.1 into GAC so that the system can it.



  • Run Developer Command Prompt for VS2019 as Administrator



  • type:


    cd xxxxx (the path of the the System.Runtime.CompilerServices.Unsafe 4.0.4.1)

    gacutil /i System.Runtime.CompilerServices.Unsafe.dll



2) If you use Net Framework projects with xxx.config file, you could use bindingRedirect.


Add these in app.config file or web.config file:


<configuration>  
<runtime>
<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.4.1"
newVersion="4.0.4.1"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

Besides, if you update System.Runtime.CompilerServices.Unsafe nuget package version to the newer version, you should also changed the bindingRedirect assembly version.


You can refer to these assembly versions of System.Runtime.CompilerServices.Unsafe


4.5.x is System.Runtime.CompilerServices.Unsafe nuget package version while 4.0.x.x is System.Runtime.CompilerServices.Unsafe.dll assembly version.


4.5.0 is 4.0.4.0 
4.5.1 is 4.0.4.0
4.5.2 is 4.0.4.0
4.5.3 is 4.0.4.1
4.6.0 is 4.0.5.0
4.7.0 is 4.0.6.0
4.7.1 is 4.0.6.1


I assume that your are using .NET Framework. This error is known for ServiceStack.Redis and is tracked on GitHub. It occurs because you use libraries that depend on different versions of System.Runtime.CompilerServices.Unsafe. These transitive dependencies need to be resolved and consolidated to end up with one assembly in your output folder. You will end up with the latest of these versions. Consequently, if one of the libraries depends on a specific version that is older, it will not be found.


The bug that causes this issue is fixed in System.Runtime.CompilerServices.Unsafe 4.6.0. Use binding redirects, to load the specific version of the assembly that you need. Insert this snippet into all of your app.config files.


<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>

You need to specify the assembly version of the assembly that you need as newVersion. This is not the same as the package version that you choose when installing your NuGet package. They correspond like this:



  • Package 4.5.3 contains assembly version is 4.0.4.1

  • Package 4.7.0 contains assembly version 4.0.6.0


In this binding redirect I use the newer version of System.Runtime.CompilerServices.Unsafe that fixes the bug. However, if you depend on the older version, use 4.0.4.1.



Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable