面向 4.7.2 的 Asp.net 网站项目无法使用 MSBuild 解析 .netstandard 2.0 引用

Jac*_*ack 8 .net c# asp.net msbuild .net-core

我有一个 .net 4.0 asp.net 网站(旧网站,没有 .csproj)。这引用了 .net framework 4.0 类库(共享库 A)。

我在解决方案中添加了一个新的 .net 核心 API 项目。该项目还需要与共享库 A 对话。

解决方案(我认为)是将类库移动到 .net 标准 2.0,然后将网站移动到 4.7.2,这将允许它引用 .net 标准 2.0 项目。

为此,我更改了网站的网络配置,以便:

 <compilation debug="true" targetFramework="4.7.2">
 <httpRuntime maxRequestLength="10000" executionTimeout="1000" requestValidationMode="2.0" targetFramework="4.7.2"/>
Run Code Online (Sandbox Code Playgroud)

这一切都适用于 Visual Studio。解决方案构建、网站运行以及 .netstandard2.0 库中的代码按预期运行并且可调试。我知道 Visual Studio 做一些与 MSBuild.exe 不同的事情。

问题是我们的 CI 服务器无法构建解决方案,本地的“MSBuild.exe”也无法构建。

我正在运行的命令是:

"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe" "[Path to Solution]\AT3.sln"
Run Code Online (Sandbox Code Playgroud)

然后我得到错误:

 warning MSB3268: The primary reference "C:\Work\AT3\at3\wrld\AT3.Common.Utilit ies\bin\Debug\netstandard2.0\AT3.Application.Utilities.dll" could not be resolved because it has an indirect depende ncy on the framework assembly "netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" which  could not be resolved in the currently targeted framework. ".NETFramework,Version=v4.7.2". To resolve this problem,  either remove the reference "C:\Work\AT3\at3\wrld\AT3.Common.Utilities\bin\Debug\netstandard2.0\AT3.Application.Uti lities.dll" or retarget your application to a framework version which contains "netstandard, Version=2.0.0.0, Cultur e=neutral, PublicKeyToken=cc7b13ffcd2ddd51"
Run Code Online (Sandbox Code Playgroud)

为了尝试解决这个问题,我在我的 web.config 部分添加了以下行:

 <add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/>
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何使用 MSBuild.exe 解决这个 .netstandard2.0 项目?网站实际上可以是“4.7.2”并成功引用 .net 标准,还是 Visual Studio 说它可以对我撒谎?

注意:我知道我们需要将网站移至网络项目。我只是希望这不是这个问题的唯一解决方案,因为它有点像一个单一的网站。我们的计划是开始慢慢地将代码从 App_Code 迁移到 .netstandard2.0 库中。

先感谢您。

小智 5

我刚刚从同事那里得知。

您可以尝试在 .Net 标准库的 csproj 文件中添加引用,如下所示。这对我有用。

<Reference Include="netstandard">
  <Private>True</Private>
</Reference>
Run Code Online (Sandbox Code Playgroud)