c# - Dotnet publish 在 Debug 目录而不是 Release 目录中寻找依赖项

标签 c# asp.net-core dotnet-publish

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

dotnet restore && dotnet build -c Release

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

 dotnet publish -c Release -o published --no-restore --no-build ./Project.Api

我收到这个错误:

/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]

根据错误,它正在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>

知道为什么它在 Debug 目录而不是 Release 目录中查找吗? 在 Mac 和 Linux 机器上获取它。

最佳答案

tl;dr 使用 <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration> 创建发布发布配置文件属性并在 dotnet publish 命令中指定发布配置文件,即

dotnet publish -c Release /p:PublishProfile=Release -o published --no-restore --no-build ./Project.Api

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

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

输入 LastUsedBuildConfiguration 属性。此属性可以在发布配置文件中设置,或在生成步骤期间由 MSBuild 动态设置。我没有深入研究 SDK,但这是我怀疑正在发生的事情。由于我们跳过构建步骤,因此未设置 LastUsedBuildConfiguration,因此不可用于 dotnet publish。在这种情况下,dotnet publish 假定其 default build configuration , 即调试。

为了对此进行测试,我运行了以下 dotnet publish 命令:

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

dotnet publish -c Release --no-restore --no-build

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

dotnet publish --no-restore --no-build /p:PublishProfile=Release

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

dotnet publish -c Release --no-restore --no-build /p:PublishProfile=Release

仅在最后一种情况下,当 -c Release 和/p:PublishProfile=Release 都是使用 bin/Release 目录的 dotnet publish 时。

这对我来说已经解决了,希望它也能帮助其他人。

关于c# - Dotnet publish 在 Debug 目录而不是 Release 目录中寻找依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53720172/

相关文章:

c# - Entity Framework ,防止重复记录,同时连接

c# - WindowsPhone7 : How to animate position of UIElement with code?

asp.net-core - 如何唯一标识 ASP.NET Core 2 应用程序中的每个请求(用于日志记录)

asp.net-core - .NET Core 锁定文件

.net-core - "MCD"命令创建的 `dotnet publish` 配置是什么?

c# - 将数组与 SQL Server 数据库的 "very large"表进行比较

c# - 应用程序池存储在哪里

asp.net - 角色动态授权 asp.net core

json.net - 不同的 Newtonsoft.Json.dll dotnet 发布与使用 Visual Studio 发布