【问题标题】:Dotnet publish looking for dependency in Debug directory instead of ReleaseDotnet 发布在 Debug 目录而不是 Release 中寻找依赖项
【发布时间】:2018-12-11 08:33:14
【问题描述】:

我有一个项目 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 机器上都可以使用它。

【问题讨论】:

  • 项目属性中的构建部分怎么样?您实际上有什么?您为发布模式指定了哪个输出位置?
  • 两个项目的发布路径都是`bin\Release\netcoreapp2.1`。我可以确认这也是人工制品的实际去处。
  • 或者你的一个引用(一个.dll)可能是从调试目录添加的?
  • 我也这么认为,但这是唯一的参考 而 DOmainModels 没有
  • 移除--no-build标志

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


【解决方案1】:

您的错误是--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]

成功发布:

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\

答案样本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

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

【讨论】:

  • 感谢 Soheil,不确定我是否遗漏了什么,但在我做 dotnet publish 之前我做 dotnet build -c Release,大概这应该创建依赖 dll(它会创建并放置在 Release 目录中) .只是该版本不在该目录中查找,而是在 Debug 目录中查找。
  • @SelloMkantjwa 你能把你的代码推送到 github 上吗?
  • 我做到了,它有效 - 谢谢。看看stackoverflow.com/questions/53720172/…。它并没有真正回答根本问题,即为什么它在 Debug 目录而不是 Release 中查找? PS 你得到的错误和我得到的错误不一样。
  • @SelloMkantjwa 我明白了。
  • @SelloMkantjwa,这可能是 dotnet 系统中的问题,您要报告吗?
【解决方案2】:

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

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

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

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

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

为了对此进行测试,我运行了以下 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 发布时。

这是为我整理的,希望它也可以帮助其他人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多