我花了几个小时试图弄清楚为什么 .Net Core 2.0 不会加载 .Net framework 4.5.2 nuget 包。
现在我想是时候问了...
发生的情况是我有一个 .Net Core 2.0 WebApi 应用程序 (A),我想重用我的 .Net 框架 4.5.2 库 (B)。
我为 B 创建了 nuget 包,并在 A 中引用了 nuget 包。
库下载在 C:\Users\username.nuget\packages 文件夹中
但是当我在代码中使用 B 时,它错误地告诉我
System.IO.FileLoadException: 'Could not load file or assembly 'xxx.TradeServices.Common, Version=2.1.289.1, Culture=neutral, PublicKeyToken=null'. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'
然后我添加了一些虚拟代码
var client = new TradeServicesClient(EnvironmentType.Production);
xxx.TradeServices.Common 已加载。
但是出现了另一个异常
说无法加载依赖dll。
FileNotFoundException: Could not load file or assembly 'Ice, Version=3.7.0.0, Culture=neutral, PublicKeyToken=0c5ebb72d74932c6'. The system cannot find the file specified.
我已经检查了所有依赖项,它们在
C:\Users\username.nuget\packages
文件夹
我不确定 .Net Core 为什么会这样。似乎没有加载依赖链。你能帮我弄清楚发生了什么吗?
最佳答案
asp.net core 可以针对多个框架;例如,2.0 Web 应用程序通常会在“netcoreapp2.0”(.NET Core 应用程序)或“net461”(.NET 应用程序)上运行 - 正如 <TargetFramework>
所指定的那样在 csproj 中。是这里<TargetFramework>
这决定了所有下游包解析将如何工作。如果是“net461”,拿个“net452”库可能会很开心。但是,“netcoreapp2.0”不需要“net452”——而是更喜欢“netstandard2.0”或“netstandard1.6”等。定位 .NET Standard 意味着所有下游包也需要定位 .NET Standard,即并不总是可能的。
所以:
<TargetFramework>
是“net461”或类似的 编辑:看起来默认项目也在
Microsoft.AspNetCore
之间发生变化(针对 .NET 时)和 Microsoft.AspNetCore.All
(针对 .NET Core 时) - 所以您可能还需要更改 <PackageReference ... />
csproj 中的条目。如果您正在使用 Microsoft.AspNetCore.All
中的任何额外包不在 Microsoft.AspNetCore
- 您可能需要手动添加您需要的那些。
关于.net - 从 .Net Core 2.0 项目引用 .Net framework 4.5.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49534543/