请检查以下是否属于您的情况。
有时,我们会遇到解决方案的不同部分(项目)依赖于同一 DLL 的不同版本的情况,即;具有相同程序集名称的程序集。
错误
无法加载文件或程序集
'System.Runtime.CompilerServices.Unsafe,版本=4.0.4.1,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 或其之一
依赖关系。定位程序集的清单定义不
匹配程序集引用。 (HRESULT 异常:0x80131040)
建议,您的项目正在寻找程序集版本 4.0.4.1
解决方法
我。通常 Nuget 包 4.5.3 包含程序集版本 4.0.4.1。
请检查您的解决方案中的两个项目是否可以在该程序集版本上工作,并通过binding redirect 将该特定版本作为新版本(例如:4.0.4.1)添加到您遇到错误的项目中。并保留旧版本代替旧版本。
还可以右键单击包引用并在其属性下将“特定版本”设置为 false
例子:
<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>
在此示例中,这种添加方式指定运行时应在旧版本范围 0.0.0.0-4.0.6.0 之间的 Assembly 版本中使用版本 4.0.6.0
否则,如果不是上述情况,则解决方案可能需要不同的版本。
ii.
-
在项目属性中右击,选择ApplicationConfiguration文件和App.config
-
在 app.config 文件中添加以下设置。
通过codeBases配置多个同名程序集。
一些包的示例代码说“A”:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="A " publicKeyToken="3d67ed1f87d44c89" />
<codeBase version="3.0" href="...\A.dll"/>
<codeBase version="5.0" href="...\A.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
三。看看你能不能用extern alias解决这个问题。
四。如果您的应用程序或其组件引用同一程序集的多个版本,则会添加绑定重定向。请参阅Enable or disable autogenerated binding redirects | Microsoft Docs。如果您手动执行此操作,则需要在项目属性下禁用。
您可以手动在属性组的 csproj 文件中添加<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>。
例如:
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
参考资料可能会有所帮助。