簡體   English   中英

netstandard2.0(.net standard 2.0)類庫不能引用net461(.net framework 4.6.1)類庫

[英]netstandard2.0 (.net standard 2.0) class library cannot reference a net461 (.net framework 4.6.1) class library

首先很抱歉,如果之前有人問過這個問題。 我已經搜索並搜索了netstandard2.0已經發布的很多沖突和過時的信息。

我正在學習新的netstandard和核心遷移,並使用CLI dotnet命令構建了空的vanilla“hello world”項目。 有Nuget參考。

問題是從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"

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 -標准

另外,Immo Landwerth [MSFT]在他的文章中似乎通過兼容性墊片提出了可能性,我認為可能只有Nuget參考:

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

在此輸入圖像描述

(也許Scott Hanselman可以幫忙!)

似乎netsandard2.0支持net461,“.NET實現支持”網格顯示了這一點。

https://docs.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

在上面克隆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>

這是否有效,因為現在兩個classlib都以netstandard2.0為目標

問題

  • 是否有一個簡單的圖表,用簡單的術語來揭開所有這一切的神秘面紗!

  • 為什么這不起作用。 如果一個空的netstandard2.0實現了net461的大部分,那么它肯定擁有支持裸網絡461引用所需的一切。 我知道有一些api是行不通的,即WPF,但是這些是裸項目,沒有Nuget或直接二進制引用。

  • 解決方法,為什么這樣做? 我們是否總是需要在classlib中同時使用net461和netstandard2.0,以便netstandard2.0只能引用classlib來引用它們。

  • 從netstandard2.0 classlib中使用舊的.net framwork classlibs(net4.3以上)的正確結構是什么。

  • 這怎么工作?

    https://blogs.msdn.microsoft.com/dotnet/2017/06/28/announcing-net-core-2-0-preview-2/#user-content-reference-net-framework-libraries-from-net -標准

    在此輸入圖像描述

對引用.NET Framework庫的.NET Standard 2.0+和.NET Core 2.0+項目的支持目前僅限於NuGet包(通過特殊的回退定義),這就是為什么它不適用於項目到項目的引用。

當MSBuild為此使用相同的解決方案策略時,這可能會在即將推出的工具版本中發生變化。 請參閱此跟蹤GitHub問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM