.NetCore 运行时标识符参数被忽略、MSBuild、Team City

Dav*_*504 1 msbuild teamcity visual-studio asp.net-core

我一直在尝试通过 Team City 构建和发布 MSBuild 时遇到问题。收到的错误信息如下:

C:\Program Files\dotnet\sdk\2.2.402\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(208, 5): error NETSDK1047: Assets file 'F:\TeamCity\buildAgent\work\9f1e0cb4e5e5076f\myproject\obj\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v2.2/win-x64'. Ensure that restore has run and that you have included 'netcoreapp2.2' in the TargetFrameworks for your project. You may also need to include 'win-x64' in your project's RuntimeIdentifiers.
Run Code Online (Sandbox Code Playgroud)

我原以为将参数中的以下内容传递给 MSBuild 应该可以解决问题。

/p:TargetFramework=netcoreapp2.2 /p:Runtimeidentifier=win-x64
Run Code Online (Sandbox Code Playgroud)

然而事实并非如此。

我可以从 Visual Studio 中构建/发布项目,但不能通过 Team City 构建代理。

为了让构建代理构建/发布,我必须在项目 .csproj 文件中声明运行时标识符:

<PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

在我看来,要么 Team City 忽略了 MSBuild 参数,我的参数不正确,要么 Visual Studio 正在执行一个我不知道的附加步骤,以在发布时声明运行时标识符(通过 WebDeploy)。

我显然已经解决了我遇到的问题,但这似乎不是“正确”的做法。我错过了什么?

谢谢,

Lan*_*SFT 6

我显然已经解决了我遇到的问题,但这似乎不是“正确”的做法。我错过了什么?

你可能会从这个博客中得到一些帮助,project.assets.json列出了项目的所有依赖项

当你.net core在 VS 中创建一个新项目时,它会为你创建这个文件。用记事本打开,发现默认没有指定运行时间:

在此处输入图片说明

当您尝试通过 msbuild 命令行使用 构建它时,msbuild/p:Runtimeidentifier=win-x64无法识别该运行时,因为它未在projects.assets.json文件中定义。

解决此问题的一种简单方法是-restore在使用Runtimeidentifier=xxx.

所以实际上你需要将以下参数传递给msbuild,不需要手动编辑xx.csproj文件:

/p:TargetFramework=xxx /p:Runtimeidentifier=xxx /restore
Run Code Online (Sandbox Code Playgroud)

msbuild会restore在运行target之前先执行buildtarget,restoretarget会projects.assets.json为win-64重新生成一个新文件,见:

在此处输入图片说明

解决方案

所以正确的方法是在restore你的构建命令中添加一个开关,然后无论Runtimeidentifier你想以哪个为目标,构建都会成功。避免手动编辑Runtimeidentifier项目文件!

至于为什么它在VS中有效:当我们使用发布功能(右键单击项目=>发布)时,VS会自动为我们做恢复工作。我们可以很容易地在 path 中找到 win-64 的 projects.assets.json ProjectFolder\obj\publish\win-x64,因此当您从 VS 发布时它会成功。嗯,这就是IDE的魅力吧?