43

I created a Visual Studio (Community 2019) project with C# using ServiceStack.Redis. Since it is C#, I use Windows 10 (there is a Redis version for Windows but it is really old and as I know, it is unofficial so I am afraid that might be the problem). Here is an excerpt from my code:

public class PeopleStorage: IDisposable
{
    public PeopleStorage()
    {
        redisManager = new RedisManagerPool("localhost");
        redis = (RedisClient)redisManager.GetClient();
        facts = (RedisTypedClient<List<Fact>>)redis.As<List<Fact>>();
    }

    public List<Fact> GetFacts(int id)
    {
        string sid = id.ToString();
        if (facts.ContainsKey(sid))
            return facts[sid];
        return accessor.GetFacts(id);
    }

    private RedisTypedClient<List<Fact>> facts;
    private RedisClient redis;
    private RedisManagerPool redisManager;
}

In an attempt to connect to Redis in line return facts[sid];, an exception occurs:

System.IO.FileLoadException: "Could not load file or assembly "System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" or one of it's dependences. The found Assembly's manifest definition does not match the Assembly reference. (Exception from HRESULT: 0x80131040)"

(May be inaccurate as I have translated it)

I have tried updating all the packages, starting with ServiceStack packages, ending with System.Runtime.CompilerServices.Unsafe itself. Moreover, you can't choose 4.0.4.1 version in NuGet, the closest one there is 4.0.0, while the relevant is 4.0.7.

I do not understand why it uses this version and how I can fix this problem.
Even a clean reinstall of Visual Studio did not help.

thatguy
  • 18,288
  • 6
  • 23
  • 36
Rabter
  • 543
  • 1
  • 4
  • 6

8 Answers8

54

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
5.0.0 is 5.0.0.0
Sedat Kapanoglu
  • 44,742
  • 25
  • 114
  • 143
Mr Qian
  • 17,533
  • 1
  • 15
  • 28
  • So I did not really understand your explanation since i have tried using both 4.0.7 and 4.5.3 packages but your first suggestion worked, thank you! Although now I can't try the second one... P.S. need 2 more points to upvotethis answer :( – Rabter Jul 07 '20 at 11:14
  • #1 suggestion worked for me too. I wonder why it has to be cached on my other laptop since everything's working well with my main laptop. – Eraniichan Oct 10 '20 at 18:15
  • 6
    4.7.0 is 4.0.6.0, where did you find this mapping? Did you download them one by one, and check it by tools like dnSpy? – Chuck Lu Nov 05 '20 at 10:07
  • 1
    Thanks, solution 2 worked for me, but I think there is a mistake in your snippet. It should be publicKeyToken="b03f5f7f11d50a3a" – Stefano Altieri Dec 03 '20 at 15:23
  • @StefanoAltieri, thanks for pointing that. And I will update it. – Mr Qian Dec 04 '20 at 01:36
  • 3
    how does one even find the assembly version? – maembe Jan 06 '21 at 18:06
  • I am on NuGet.org, right now... That is NOT what I am seeing... 4.52 of Nuget Package has 4.6.28619.1 as DLL version. – David V. Corbin Feb 08 '21 at 10:18
  • To find the version yourself, go to "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools" where v10.0A could be a different number so check first. There you will find ildasm.exe. Start it and open the dll you wanna check the version for. – iBobb Feb 11 '21 at 18:24
  • You can use [Nuget Package explorer](https://github.com/NuGetPackageExplorer/NuGetPackageExplorer) to discover assembly versions. – Sedat Kapanoglu Oct 16 '21 at 00:06
18

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.

thatguy
  • 18,288
  • 6
  • 23
  • 36
8

Based on Perry's answer, I simply installed the nuget package System.Runtime.CompilerServices.Unsafe version 4.5.3 and the problem was solved.

datchung
  • 2,490
  • 22
  • 21
5

My problem was solved by deleting the "bin" and "obj" folders.

Tugay ÜNER
  • 67
  • 1
  • 4
1

Recently I encountered exactly the same error message. However, that message did not appear in all PCs that I used to test my app. Some PCs yielded the error message but some others are not. I couldn't differentiate which are the characteristics of PC that would be generating error message and which ones did not. It seemed.. random.

Then I noticed a warning message when I compiled my app (I was using Visual Studio 2019 at the time):

Severity Code Description Project File Line Suppression State Warning Found conflicts between different versions of the same dependent assembly. Please set the "AutoGenerateBindingRedirects" property to true in the project file. For more information, see http://go.microsoft.com/fwlink/?LinkId=294190. SQL Online Exam System

And I did exactly what it told me:

enter image description here

Then the problem was solved.

yunhasnawa
  • 795
  • 12
  • 27
0

In my experience, I didn't need to install System.Runtime.CompilerServices.Unsafe because it is referenced from another package, npgsql. Instead, what I did is as follow:

  1. Insert this code snippet to app.config of the project
 <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.6.0" newVersion="4.0.6.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="4.1.4.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
            <bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1"/>
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
  1. Run again the project

This is the solution I found so far, to add dependentAssembly bindingReference

farizmamad
  • 131
  • 1
  • 5
0

From my bad experience: i was adding project to solution, which already have "System.Runtime.CompilerServices.Unsafe" Version="4.5.0" When i started with Microsoft.Toolkit.Mvvm NuGet, it try retrieve "System.Runtime.CompilerServices.Unsafe" > 5.0.0, among other DLLs. It makes hell. So, i moved to MVVM Cross and i happy.

zzfima
  • 1,429
  • 1
  • 14
  • 21
0

Thanks @thatguy finally was able to resolve the issue.

The solution that worked for me was from Nick Craver with details as below:

Things to try:

** Reference the System.Runtime.CompilerServices.Unsafe library directly as a (so VS alerts it needs a binding and offers a one-click fix...sometimes it won't realize this transitively).

** If using app.config, remove all binding redirects and add a true property up on the top.


This is a load errors and I really really really wish they had a wiki page explaining how to fix this because it can plague any library anyway if you step on one of the magical landmines.

Kasim Husaini
  • 372
  • 4
  • 13