首页 > 解决方案 > Visual Studio .NET Standard 库和 .NET Framework 项目 - NuGet dll 清单定义不匹配

问题描述

我使用 VS 2019,我有一个名为 Foo 的 .NET Standard (2.0) 库项目(使用 ServiceStack NuGet 包)和一个名为 Bar 的主 .NET Framework (4.7.2) Windows 服务项目(也使用 ServiceStack NuGet)。Bar 引用了 Foo。Bar 是一个 Windows 服务和一个 ServiceStack REST 主机,Foo 具有跨解决方案共享的 DTO(它需要 ServiceStack 库用于 [Route] 属性)。

当我尝试在 Bar 中运行任何与 ServiceStack 相关的代码时,我得到:

{“无法加载文件或程序集 'ServiceStack.Interfaces, Version=5.0.0.0, Culture=neutral, PublicKeyToken=null' 或其依赖项之一。定位程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常: 0x80131040)":"ServiceStack.Interfaces,版本=5.0.0.0,文化=中性,PublicKeyToken=null"} System.IO.FileLoadException

尽管在 Bar 的 app.config 中有这个条目(我使用来自 Bar 的 .csproj 的 publicKeyToken):

  <dependentAssembly>
    <assemblyIdentity name="ServiceStack.Interfaces" publicKeyToken="02c12cbda47e6587" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
  </dependentAssembly>

我怎样才能让这两者一起工作?

编辑:

Foo 和 Bar 中的 ServiceStack NuGet 包都有版本5.5.0(在 VS 属性中看到的 .NET Framework 的 .dll“版本”是5.0.0.0)。我尝试过使用和不使用publicKeyToken,我尝试过映射oldVersion="0.0.0.0-5.5.0.0" newVersion="5.0.0.0"oldVersion="0.0.0.0-5.5.0.0" newVersion="5.5.0.0".

输出文件夹中的实际 .dll 具有以下全名:ServiceStack.Interfaces, Version=5.0.0.0, Culture=neutral, PublicKeyToken=02c12cbda47e6587

标签: c#.netvisual-studionuget.net-standard

解决方案


您可以搜索“ServiceStack 多目标”以查看解决此问题的答案,例如 this answer

总之,如果您只想在 .NET Framework 上运行 ASP.NET Core 应用程序,只能引用 .Core 包,否则如果您希望能够在同一个解决方案中将 .NET Standard 和 .NET Framework 项目混合在一起,您需要多目标


推荐阅读