从.Net Core 2.0项目参考.Net框架4.5.2 [英] reference .Net framework 4.5.2 from .Net Core 2.0 project

查看:171
本文介绍了从.Net Core 2.0项目参考.Net框架4.5.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几个小时试图弄清楚为什么.Net Core 2.0无法加载.Net Framework 4.5.2 nuget程序包.

I have spent a few hours trying to figure out why .Net Core 2.0 wouldn't load .Net framework 4.5.2 nuget packages.

现在我想是时候问了...

Now I think it's time to ask...

发生了什么事,我有一个.Net Core 2.0 WebApi应用程序(A),我想重用我的.Net Framework 4.5.2库(B).

What happens is I have a .Net Core 2.0 WebApi app (A) and I want to reuse my .Net framework 4.5.2 library(B).

我为B创建了nuget程序包,并在A中引用了nuget程序包.

I created nuget packages for B and referenced the nuget package in the A.

库下载到C:\ Users \ username.nuget \ packages文件夹中 但是当我在代码中使用B时,我误以为是

The libraries are downloaded in the C:\Users\username.nuget\packages folder But when I consume B in the code, it errors me out saying

System.IO.FileLoadException:'无法加载文件或程序集 'xxx.TradeServices.Common,版本= 2.1.289.1,文化=中性, PublicKeyToken = null".找到的程序集的清单定义确实 与程序集参考不匹配. (来自HRESULT的异常: 0x80131040)'

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

然后我添加了一些伪代码

I then added some dummy code

var client = new TradeServicesClient(EnvironmentType.Production);

xxx.TradeServices.Common已加载.

xxx.TradeServices.Common is loaded.

但是出现另一个异常 说不能加载依赖项dll.

But another exception appear saying cannot load dependency dll.

FileNotFoundException:无法加载文件或程序集"Ice, 版本= 3.7.0.0,区域性=中性,PublicKeyToken = 0c5ebb72d74932c6'. 系统找不到指定的文件.

FileNotFoundException: Could not load file or assembly 'Ice, Version=3.7.0.0, Culture=neutral, PublicKeyToken=0c5ebb72d74932c6'. The system cannot find the file specified.

我已经检查了所有依赖项,它们已正确下载到

I have checked all the dependencies, they are downloaded correctly in the

C:\ Users \ username.nuget \ packages

C:\Users\username.nuget\packages

文件夹

我不确定.Net Core为何采用这种方式.似乎没有加载依赖项链.您能帮我弄清楚发生了什么吗?

I am not sure about why .Net Core is behaving this way. It seems the dependency chain is not loaded. Could you please help me to figure out what's going on?

推荐答案

asp.net核心可以针对多个框架;因此,例如,2.0 Web应用程序通常将在"netcoreapp2.0"(.NET Core应用程序)或"net461"(.NET应用程序)上运行-由csproj中的<TargetFramework>指定. <TargetFramework>决定了所有下游程序包分辨率将如何工作.如果它是"net461",那么选择"net452"库可能会很高兴.但是,"netcoreapp2.0"将不希望使用"net452",而是希望使用"netstandard2.0"或"netstandard1.6"等.针对.NET Standard,这意味着所有下游包 也需要目标.NET标准,但这并不总是可能的.

asp.net core can target multiple frameworks; a 2.0 web app will typically run on either "netcoreapp2.0" (a .NET Core application) or "net461" (a .NET application), for example - as specified by the <TargetFramework> in the csproj. It is this <TargetFramework> that determines how all the downstream package resolution will work. If it is "net461", it may be happy to take a "net452" library. However, "netcoreapp2.0" will not want "net452" - instead preferring "netstandard2.0" or "netstandard1.6", etc. Targeting .NET Standard will mean that all downstream packages also need to target .NET Standard, which is not always possible.

所以:

  • 如果可能的话,使您的依赖项针对某个版本的.NET Standard
  • 如果无法实现,请确保<TargetFramework>是"net461"或类似版本
  • if possible, make your dependencies target some version of .NET Standard
  • if that isn't possible, ensure that the <TargetFramework> is "net461" or similar

看起来默认项目Microsoft.AspNetCore(面向.NET时)和Microsoft.AspNetCore.All(面向.NET Core时)之间切换-因此,您可能还需要更改它csproj中的<PackageReference ... />条目.如果您使用的是Microsoft.AspNetCore.All中没有的任何其他软件包,则可能需要手动添加所需的软件包.

it looks like the default projects also change between Microsoft.AspNetCore (when targeting .NET) and Microsoft.AspNetCore.All (when targeting .NET Core) - so you may also need to change that <PackageReference ... /> entry in the csproj. If you are using any of the extra packages in Microsoft.AspNetCore.All that aren't in Microsoft.AspNetCore - you may need to add the ones that you need manually.

这篇关于从.Net Core 2.0项目参考.Net框架4.5.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆