所以我有一个共享的公共(public)库,它是引用 RestSharp。我使用 NuGet 安装 RestSharp,所以它是引用版本 106.3.1。现在,我已将 Specific Version 设置为 False,从 csproj 文件引用中删除了版本号,并将 Private 设置为 true。
<Reference Include="RestSharp">
<HintPath>..\packages\RestSharp.106.3.1\lib\net452\RestSharp.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
<Private>True</Private>
</Reference>
然后从我的主要 .NET Framework 4.6.1 Web 应用程序引用这个公共(public)库。该 Web 应用程序反过来引用了 NuGet 的另一个库,该库对 RestSharp 105.1.0.0 具有硬版本依赖性。由于依赖于此,Web 应用程序现在还引用 RestSharp 105.1.0.0。
现在,根据我对 csproj 程序集引用的理解,这应该可以工作。它没有。当我运行它时,当 Common 库(引用 RestSharp 106.3.1 的代码)执行时,我在运行时收到此错误:
System.IO.FileLoadException: 'Could not load file or assembly 'RestSharp, Version=106.3.1.0, Culture=neutral, PublicKeyToken=598062e77f915f75' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'
总而言之,我从我的主要项目中引用了两个不同的项目。这两个项目都依赖于 RestSharp,但版本不同。我认为上述 csproj 更改应该可以修复它,但它不起作用。
我需要更改什么才能修复此 DLL hell 问题?非常感谢此处的任何帮助。
更新:我按照建议尝试了绑定(bind)重定向,如下所示:
<dependentAssembly>
<assemblyIdentity name="RestSharp" publicKeyToken="598062e77f915f75" culture="en-us" />
<bindingRedirect oldVersion="105.1.0" newVersion="106.3.1.0" />
</dependentAssembly>
不幸的是,这没有用,因为 RestSharp 105.1.0 没有 publicKeyToken,所以现在我收到一条错误消息,指出找不到 105.1.0 版本的库。还有其他想法吗?
更新 2: 尝试删除 publickeytoken 和 culture:
<dependentAssembly>
<assemblyIdentity name="RestSharp"/>
<bindingRedirect oldVersion="105.1.0.0" newVersion="106.3.1.0" />
</dependentAssembly>
这也没有用,产生:
FileLoadException: Could not load file or assembly 'RestSharp, Version=105.1.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
最佳答案
我也有同样的疑问。
关键是105.1.0的publicKeyToken为null,连api都和106.3.1.0不一样。所以它们不兼容,不能使用 bingding 重定向。
我找到了一个答案,但我没有验证它,因为它不能在 nuget 下。不介意的话可以试试。
<dependentAssembly>
<assemblyIdentity name="RestSharp" publicKeyToken="null" culture="neutral" />
<codeBase version="105.1.0.0" href="RestSharp.105.1.0/RestSharp.dll" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="RestSharp" publicKeyToken="598062e77f915f75" culture="neutral" />
<codeBase version="106.3.1.0" href="RestSharp.106.3.1/RestSharp.dll" />
</dependentAssembly>
关于c# - 程序集引用版本冲突——如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51912800/