无法加载文件或程序集system.runtime.compilerservices.unsafe

uemypmqf  于 2021-06-10  发布在  Redis
关注(0)|答案(2)|浏览(4492)

我创建了一个visualstudio(community2019)项目,其中c#使用 ServiceStack.Redis . 因为它是c#,所以我使用windows10(有一个windows的redis版本,但它确实很旧,而且据我所知,它是非官方的,所以我担心这可能是问题所在)。以下是我的代码摘录:

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

试图连接到redis return facts[sid]; ,出现异常:
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。
我不明白为什么它使用这个版本,我如何才能解决这个问题。
即使重新安装visualstudio也无济于事。

svdrlsy4

svdrlsy41#

无法加载文件或程序集system.runtime.compilerservices.unsafe
似乎已安装system.runtime.compilerservices.unsafe nuget包 4.5.3 版本。它对应于 System.Runtime.CompilerServices.Unsafe.dll 程序集版本 4.0.4.1 .
建议
1) 请尝试注册 System.Runtime.CompilerServices.Unsafe 版本 4.0.4.1 这样系统就可以。
以管理员身份运行vs2019的开发人员命令提示符
类型:

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

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

2) 如果将net framework项目用于 .config 文件,可以使用bindingredirect。
把这些加进去 app.config 文件或 web.config 文件:

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

另外,如果你更新 System.Runtime.CompilerServices.Unsafe 如果要将nuget包版本更改为较新版本,还应更改bindingredirect程序集版本。
您可以参考
System.Runtime.CompilerServices.Unsafe 4.5.xSystem.Runtime.CompilerServices.Unsafe nuget包版本 4.0.x.xSystem.Runtime.CompilerServices.Unsafe.dll 程序集版本。

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
m2xkgtsf

m2xkgtsf2#

我假设您使用的是.net framework。此错误以 ServiceStack.Redis 在github上被追踪。发生这种情况是因为您使用的库依赖于的不同版本 System.Runtime.CompilerServices.Unsafe . 需要解析和合并这些可传递的依赖项,以便在输出文件夹中得到一个程序集。您将得到这些版本中的最新版本。因此,如果其中一个库依赖于较旧的特定版本,则不会找到它。
导致此问题的错误已在中修复
System.Runtime.CompilerServices.Unsafe 4.6.0 . 使用绑定重定向加载所需程序集的特定版本。将此代码段插入您的所有 app.config 文件夹。

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

您需要指定所需的程序集的程序集版本 newVersion . 这与安装nuget包时选择的包版本不同。它们对应如下:
包4.5.3包含的程序集版本为4.0.4.1
包4.7.0包含程序集版本4.0.6.0
在这个绑定重定向中,我使用的是 System.Runtime.CompilerServices.Unsafe 修复了错误。但是,如果您依赖旧版本,请使用 4.0.4.1 .

相关问题