[英]netstandard2.0 (.net standard 2.0) class library cannot reference a net461 (.net framework 4.6.1) class library
Firstly sorry if this has been asked before. 首先很抱歉,如果之前有人问过这个问题。 I have searched and searched there is a lot conflicting and outdated information as netstandard2.0 has been released. 我已经搜索并搜索了netstandard2.0已经发布的很多冲突和过时的信息。
I am learning the new netstandard and core migrations and have built empty vanilla "hello world" projects using CLI dotnet commands. 我正在学习新的netstandard和核心迁移,并使用CLI dotnet命令构建了空的vanilla“hello world”项目。 There are Nuget references. 有Nuget参考。
The issue is adding a reference from a netstandard2.0 classlib to a net461 classlib causes an error (netstandard2.0 --> net461 reference) 问题是从netstandard2.0 classlib添加引用到net461 classlib导致错误(netstandard2.0 - > net461引用)
"cannot be added due to incompatible targeted frameworks between the two projects. Please review the project you are trying to add and verify that is compatible with the following targets"
This microsoft article by Rich Lander claims that I can do this with netstandard2.0 Rich Lander的这篇微软文章声称我可以用netstandard2.0做到这一点
https://blogs.msdn.microsoft.com/dotnet/2017/06/28/announcing-net-core-2-0-preview-2/#user-content-reference-net-framework-libraries-from-net-standard https://blogs.msdn.microsoft.com/dotnet/2017/06/28/announcing-net-core-2-0-preview-2/#user-content-reference-net-framework-libraries-from-net -标准
Also Immo Landwerth [MSFT] in his article seemed to allude possibility via a compatibility shim, which I believe maybe by Nuget reference only: 另外,Immo Landwerth [MSFT]在他的文章中似乎通过兼容性垫片提出了可能性,我认为可能只有Nuget参考:
https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/ https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/
(perhaps Scott Hanselman can help!) (也许Scott Hanselman可以帮忙!)
It appears that netsandard2.0 supports net461, the ".NET implementation support" grid shows that. 似乎netsandard2.0支持net461,“.NET实现支持”网格显示了这一点。
https://docs.microsoft.com/en-us/dotnet/standard/net-standard https://docs.microsoft.com/en-us/dotnet/standard/net-standard
Have created a GitHub repo to demonstrate this 已经创建了一个GitHub仓库来演示这个
https://github.com/raxuser100/net_standard_references.git https://github.com/raxuser100/net_standard_references/tree/master https://github.com/raxuser100/net_standard_references.git https://github.com/raxuser100/net_standard_references/tree/master
Clone master in the above and run this from a command prompt: 在上面克隆master并从命令提示符运行它:
dotnet add .\classlib_netstandard2.0_ref_classLib_net461\classlib_netstandard2.0_ref_classLib_net461.csproj reference .\classlib_net461\classlib_net461.csproj
You will then get the error. 然后,您将收到错误。
The csproj files have the following contents classlib_net461.csproj csproj文件具有以下内容classlib_net461.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net461</TargetFramework>
</PropertyGroup>
</Project>
classlib_netstandard2.0.csproj classlib_netstandard2.0.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
I have used the CLI commands below to create a project structure: 我使用下面的CLI命令创建了一个项目结构:
dotnet new console --name console_core2.0 --framework netcoreapp2.0
dotnet new classlib --name classlib_netstandard2.0 --framework netstandard2.0
dotnet new classlib --name classlib_net461 --target-framework-override net461
# netstandard2.0 --> net461 link
dotnet new classlib --name classlib_netstandard2.0_ref_classLib_net461 --framework netstandard2.0
<#
add project a reference
netstandard2.0 --> net461 link
this returns an error
cannot be added due to
incompatible targeted frameworks between the two projects. Please review the project you are trying to add and verify that is compatible with the following targets:
At line:1 char:1
Works if we edit classlib_net461.csproj and add multiple targets including netstandard2.0
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>
#>
dotnet add .\classlib_netstandard2.0_ref_classLib_net461\classlib_netstandard2.0_ref_classLib_net461.csproj reference .\classlib_net461\classlib_net461.csproj
Works if we edit classlib_net461.csproj and add multiple targets including netstandard2.0 如果我们编辑classlib_net461.csproj并添加多个目标(包括netstandard2.0),则可以正常工作
classlib_net461.csproj classlib_net461.csproj
<PropertyGroup>
<TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>
Now when we reference, it works fine as you can see below: 现在,当我们参考时,它可以正常工作,如下所示:
classlib_netstandard2.0_ref_classLib_net461.csproj classlib_netstandard2.0_ref_classLib_net461.csproj
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\classlib_net461\classlib_net461.csproj" />
</ItemGroup>
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
Does this work because now both classlibs target netstandard2.0 这是否有效,因为现在两个classlib都以netstandard2.0为目标
Is there a simple diagram to demystify all of this in simple terms with all the gotcha's! 是否有一个简单的图表,用简单的术语来揭开所有这一切的神秘面纱!
Why does this not work. 为什么这不起作用。 If an empty netstandard2.0 implements most of net461 then surely it has everything it needs to support a bare net461 reference. 如果一个空的netstandard2.0实现了net461的大部分,那么它肯定拥有支持裸网络461引用所需的一切。 I know there are some api's that do not work, namely WPF, however these are bare projects, with no Nuget or direct binary references. 我知道有一些api是行不通的,即WPF,但是这些是裸项目,没有Nuget或直接二进制引用。
Workaround, why does this work? 解决方法,为什么这样做? Do we always need to target both net461 and netstandard2.0 in classlib so that they can be referenced by netstandard2.0 only classlib. 我们是否总是需要在classlib中同时使用net461和netstandard2.0,以便netstandard2.0只能引用classlib来引用它们。
What is the correct structure for using older .net framwork classlibs (net4.3 onwards) from within a netstandard2.0 classlib. 从netstandard2.0 classlib中使用旧的.net framwork classlibs(net4.3以上)的正确结构是什么。
How did this ever work? 这怎么工作?
https://blogs.msdn.microsoft.com/dotnet/2017/06/28/announcing-net-core-2-0-preview-2/#user-content-reference-net-framework-libraries-from-net-standard https://blogs.msdn.microsoft.com/dotnet/2017/06/28/announcing-net-core-2-0-preview-2/#user-content-reference-net-framework-libraries-from-net -标准
The support for .NET Standard 2.0+ and .NET Core 2.0+ projects to reference .NET Framework libraries is currently limited to NuGet packages (via a special fallback definition), which is why it does not work for your project-to-project reference. 对引用.NET Framework库的.NET Standard 2.0+和.NET Core 2.0+项目的支持目前仅限于NuGet包(通过特殊的回退定义),这就是为什么它不适用于项目到项目的引用。
This may change in an upcoming version of the tooling when MSBuild uses the same resolution strategy for this. 当MSBuild为此使用相同的解决方案策略时,这可能会在即将推出的工具版本中发生变化。 See this tracking GitHub issue . 请参阅此跟踪GitHub问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.