Search code examples
c#visual-studioredisnugetservicestack

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


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.


Solution

  • 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

    NuGet package version Assembly version
    4.5.0, 4.5.1, 4.5.2 4.0.4.0
    4.5.3 4.0.4.1
    4.6.0 4.0.5.0
    4.7.0, 4.7.1 4.0.6.0
    5.0.0 5.0.0.0