无法加载文件或程序集 System.Runtime.CompilerServices.Unsafe [英] Could not load file or assembly System.Runtime.CompilerServices.Unsafe

查看:89
本文介绍了无法加载文件或程序集 System.Runtime.CompilerServices.Unsafe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ServiceStack.Redis 使用 C# 创建了一个 Visual Studio(社区 2019)项目.由于它是 C#,我使用 Windows 10(Windows 有一个 Redis 版本,但它真的很旧,据我所知,它是非官方的,所以我担心这可能是问题所在).这是我的代码的摘录:

公共类 PeopleStorage:IDisposable{公共人员存储(){redisManager = new RedisManagerPool("localhost");redis = (RedisClient)redisManager.GetClient();事实 = (RedisTypedClient>)redis.As>();}公开列表<事实>GetFacts(int id){字符串 sid = id.ToString();if (facts.ContainsKey(sid))返回事实[sid];返回 accessor.GetFacts(id);}私有RedisTypedClient<列表<事实>事实;私有RedisClient redis;私有RedisManagerPool redisManager;}

return fact[sid];行中尝试连接Redis,发生异常:

<块引用>

System.IO.FileLoadException: "无法加载文件或程序集"System.Runtime.CompilerServices.Unsafe,版本=4.0.4.1,文化=中性,PublicKeyToken=b03f5f7f11d50a3a"或者其中之一依赖.找到的程序集的清单定义不匹配大会参考.(来自 HRESULT 的异常:0x80131040)"

(可能是我翻译的不准确)

我尝试更新所有包,从 ServiceStack 包开始,以 System.Runtime.CompilerServices.Unsafe 本身结束.而且,NuGet 中不能选择 4.0.4.1 版本,最接近的是 4.0.0,而相关的是 4.0.7.

我不明白为什么它使用这个版本以及我如何解决这个问题.
即使干净地重新安装 Visual Studio 也无济于事.

解决方案

无法加载文件或程序集 System.Runtime.CompilerServices.Unsafe

您似乎已经安装了 System.Runtime.CompilerServices.不安全 nuget 包4.5.3 版本.并且对应于System.Runtime.CompilerServices.Unsafe.dll汇编版本4.0.4.1.

建议

1) 请尝试将System.Runtime.CompilerServices.Unsafe 版本4.0.4.1 注册到GAC 中,以便系统可以通过.

  • 管理员

    身份运行VS2019开发人员命令提示符
  • 输入:

    cd xxxxx(System.Runtime.CompilerServices.Unsafe 4.0.4.1的路径)gacutil/i System.Runtime.CompilerServices.Unsafe.dll

2) 如果您使用带有 xxx.config 文件的 Net Framework 项目,您可以使用 bindingRedirect.

将这些添加到 app.config 文件或 web.config 文件中:

<预><代码><配置><运行时><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><从属程序集><assemblyIdentity name="System.Runtime.CompilerServices.Unsafe";publicKeyToken=b03f5f7f11d50a3a"文化=中性"/><bindingRedirect oldVersion="0.0.0.0-4.0.4.1";newVersion=4.0.4.1"/></dependentAssembly></assemblyBinding></运行时></配置>

此外,如果您将 System.Runtime.CompilerServices.Unsafe nuget 包版本更新为较新版本,您还应该更改 bindingRedirect 程序集版本.

你可以参考这些System.Runtime.CompilerServices.Unsafe

的汇编版本

4.5.xSystem.Runtime.CompilerServices.Unsafe nuget 包版本,而 4.0.xxSystem.Runtime.CompilerServices.Unsafe.dll 程序集版本.

4.5.0 是 4.0.4.04.5.1 是 4.0.4.04.5.2 是 4.0.4.04.5.3 是 4.0.4.14.6.0 是 4.0.5.04.7.0 是 4.0.6.04.7.1 是 4.0.6.15.0.0 是 5.0.0.0

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.

解决方案

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

这篇关于无法加载文件或程序集 System.Runtime.CompilerServices.Unsafe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆