netstandard2.0(.net standard 2.0)类库无法引用net461(.net framework 4.6.1)类库。

3

首先,如果此前已有类似问题,敬请谅解。我已经进行了搜索,但是关于netstandard2.0的信息存在很多冲突和过时的情况。

我正在学习新的netstandard和核心迁移,并使用CLI dotnet命令构建了空的"hello world"项目。这里涉及到Nuget引用。

问题在于,从netstandard2.0类库到net461类库添加引用会导致错误(netstandard2.0 --> net461 reference)。

"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"

这篇由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

同时,Immo Landwerth [MSFT] 在他的文章中似乎提到了通过兼容性 shim 实现可能性,我认为这可能仅通过 Nuget 引用实现:

https://blogs.msdn.microsoft.com/dotnet/2016/09/26/introducing-net-standard/

enter image description here

也许Scott Hanselman可以帮忙!

似乎netsandard2.0支持net461,".NET实现支持"网格显示。

https://learn.microsoft.com/en-us/dotnet/standard/net-standard

示例代码

已经创建了一个 GitHub 仓库来演示此内容

https://github.com/raxuser100/net_standard_references.git https://github.com/raxuser100/net_standard_references/tree/master

在上述的主分支上克隆并从命令提示符中运行以下内容:
dotnet add .\classlib_netstandard2.0_ref_classLib_net461\classlib_netstandard2.0_ref_classLib_net461.csproj reference .\classlib_net461\classlib_net461.csproj

您将会收到错误提示。
csproj文件包含以下内容: classlib_net461.csproj
<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
      <TargetFramework>net461</TargetFramework>
   </PropertyGroup>
</Project>

classlib_netstandard2.0.csproj

<Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
      <TargetFramework>netstandard2.0</TargetFramework>
   </PropertyGroup>
</Project>

CLI DotNet 命令脚本

我使用以下 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

解决向classLib_net461添加多个目标的问题

如果我们编辑classlib_net461.csproj并添加包括netstandard2.0在内的多个目标,则可以解决此问题。

classlib_net461.csproj

<PropertyGroup>
    <TargetFrameworks>net461;netstandard2.0</TargetFrameworks>
</PropertyGroup>

现在当我们引用时,正如您下面所看到的那样,它能够正常工作:

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>

现在两个类库都针对netstandard2.0,这是否有效?

问题

  • 有没有一个简单的图解来以简单易懂的方式阐明所有这些问题和注意事项!

  • 为什么这样不起作用。如果一个空的netstandard2.0实现了大部分net461,那么它肯定具备支持裸的net461引用所需的一切。我知道有一些API不起作用,即WPF,但这些是裸项目,没有Nuget或直接二进制引用。

  • 解决方法,这为什么有效?我们是否总是需要针对net461和netstandard2.0来定位classlib,以便它们可以被仅netstandard2.0 classlib引用。

  • 使用较旧的.net框架类库(net4.3及以上版本)在netstandard2.0 classlib中的正确结构是什么。

  • 这是如何工作的?

    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

    enter image description here


1
.netstandard提供了一小部分在net461中可用的类。这些类可以在不同的操作系统和项目类型上工作。隐含的意思是你不能向net461程序集添加引用。因为这样做会导致生成的结果不再是netstandard。 - Hans Passant
已经提交了一个PR来解决这个问题,预计在MSBuild 15.8中修复,可以查看答案中链接的GH问题。 - Martin Ullrich
相反地,它们确保 .netstandard 程序集可以在安装了 net461 的计算机上运行。还有其他的。 - Hans Passant
它是被支持的(core / netstandard => netframework),但仅在支持的边缘,并且在与不兼容的操作系统上会失败并显示PlatformNotSupportedException。它会让你因各种警告和绑定重定向以及FileNotFoundExceptions感到疯狂,fml和System.Net.Http杀死了我的母亲。如果您使用新的csproj格式,则4.7.2会好一些,如果您升级旧项目文件,则根本不会好转。 - user1228
1个回答

0

目前,支持.NET Standard 2.0+和.NET Core 2.0+项目引用.NET Framework库的方式仅限于NuGet包(通过特殊的回退定义),这就是为什么它无法用于您的项目对项目引用。

当MSBuild使用相同的解析策略时,此情况可能会在即将推出的工具版本中发生变化。请参见this tracking GitHub issue


非常感谢您的答复。我查看了您提到的GitHub问题,但说实话不确定是否理解了它。这是否意味着这篇文章是错误的,该文章声称可以引用项目?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我想知道Rich Lander(微软)是如何在屏幕截图中做到这一点的?他使用了预览版本?今天我发推特给他,请他帮忙解决一下。 - Rax
我相信这是在预览期间发生的,NuGet从FrameworkTargetFallback转移到了AssetTargetFallback。此外,在2.0发布后(工具版本为2.1.4+左右),解析项目引用的方式(“ProjectReference protocol”)也发生了变化。但是,这个问题和已经开放的PR明确涉及(重新?)启用这种情况。 - Martin Ullrich
1
但请注意,该功能的主要目标是在.NET Core应用程序中使用现有的NuGet包。如果您的代码已经受到控制,请考虑直接针对netstandard2.0netcoreapp2.0进行开发,并在需要时利用Microsoft.Windows.Compatibility NuGet包(目前处于预览状态;它会带回一些经典API,其中一些只能在Windows上运行时才能工作)。 - Martin Ullrich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接