Dotnet 发布在 Debug 目录而不是 Release 中查找依赖项

Sel*_*jwa 6 c# asp.net-core dotnet-publish

我有一个Project.Api引用另一个项目的项目Project.DomainModel。当我通过运行构建要发布的 API 项目时

dotnet restore && dotnet build -c Release
Run Code Online (Sandbox Code Playgroud)

它构建成功。但是,当我尝试发布时

 dotnet publish -c Release -o published --no-restore --no-build ./Project.Api
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

/usr/local/share/dotnet/sdk/2.1.302/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Publish.targets(168,5): error MSB3030: Could not copy the file "xxxx/Project.DomainModels/bin/Debug/netcoreapp2.1/Project.DomainModels.dll" because it was not found. [xxxx/Project.Api/Project.Api.csproj]
Run Code Online (Sandbox Code Playgroud)

根据错误,它正在Debug目录中寻找引用的项目,但当然它不会在那里找到它,因为它会在Release目录中。

Project.Api.csproj 文件如下所示:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="AutoMapper" Version="8.0.0" />
  </ItemGroup>
  <ItemGroup>
      <ProjectReference Include="..\Project.DomainModels\Project.DomainModels.csproj" />
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

知道为什么它在 Debug 目录而不是 Release 中查找吗?在 Mac 和 linux 机器上都得到这个。

小智 6

tl;dr创建一个带有属性的发布发布配置文件<LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>,并在 dotnetpublish 命令中指定发布配置文件,即

dotnet publish -c Release /p:PublishProfile=Release -o published --no-restore --no-build ./Project.Api
Run Code Online (Sandbox Code Playgroud)

我知道这是一个老问题,但万一其他人遇到这个问题,我也遇到了类似的问题,就我而言,解决方案是确保我有一个发布发布配置文件,如果没有,则创建一个。发布配置文件包含值为 Release 的 LastUsedBuildConfiguration 属性,这似乎是关键问题。

本质上,dotnet publish -c Release我们构建然后发布发布构建配置。当我们还指定 --no-build 时,我们是说跳过构建步骤。因此,我们指定要使用的构建配置,然后告诉它不要构建。

输入 LastUsedBuildConfiguration 属性。此属性可以在发布配置文件中设置,也可以在生成步骤期间由 MSBuild 动态设置。我还没有深入研究 SDK,但我怀疑正在发生以下情况。由于我们跳过构建步骤,因此未设置 LastUsedBuildConfiguration,因此不可用于 dotnet 发布。在这种情况下,dotnetpublish 采用默认的构建配置,即 Debug。

为了测试这一点,我运行了以下 dotnetpublish 命令:

当我运行此命令时,它会在 bin/Debug 中查找(未指定 PublishProfile):

dotnet publish -c Release --no-restore --no-build
Run Code Online (Sandbox Code Playgroud)

当我运行此命令时,它会在 bin/Debug 中查找(PublishProfile,但没有 -c Release):

dotnet publish --no-restore --no-build /p:PublishProfile=Release
Run Code Online (Sandbox Code Playgroud)

当我运行此命令时,它最终会在 bin/Release 中查找(-c Release 和 PublishProfile):

dotnet publish -c Release --no-restore --no-build /p:PublishProfile=Release
Run Code Online (Sandbox Code Playgroud)

仅在最后一种情况下,当 -c Release 和 /p:PublishProfile=Release 均使用 bin/Release 目录进行 dotnet 发布时。

这为我解决了问题,希望它也能帮助其他人。


Soh*_*deh 5

你的错误是--no-build,有了这个标志,你不会让 dotnet 创建项目的引用 dll 文件。

不成功的发布:

dotnet publish -c Release -o published --no-restore --no-build .\App\

CSC : error CS0006: Metadata file 'C:\Path\App.Domain\bin\Release\netstandard2.0\App.Domain.dll' could not be found [C:\Path\App\App.csproj]
Run Code Online (Sandbox Code Playgroud)

成功发布:

dotnet publish -c Release -o published --no-restore  .\App\

 App.Domain -> C:\Path\App.Domain\bin\Release\netstandard2.0\App.Domain.dll
 App -> C:\Path\App\bin\Release\netcoreapp2.1\App.dll
 App -> C:\Path\App\bin\Release\netcoreapp2.1\App.Views.dll
 App -> C:\Path\App\published\
Run Code Online (Sandbox Code Playgroud)

答案的样本dotnet --info

.NET Core SDK (reflecting any global.json):
 Version:   2.1.500
 Commit:    b68b931422

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.17763
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\2.1.500\

Host (useful for support):
  Version: 3.0.0-preview-27122-01
  Commit:  00c5c8bc40
Run Code Online (Sandbox Code Playgroud)

阅读有关MSBuild 的更多信息,我希望这个答案可以帮助您更好地了解 dotnet 构建过程。