【问题标题】:PublishSingleFile error on upgrading from netcoreapp3.1 to net6.0从 netcoreapp3.1 升级到 net6.0 时出现 PublishSingleFile 错误
【发布时间】:2022-08-17 16:28:53
【问题描述】:

在将应用程序从 netcoreapp3.1 升级到 net6.0 后尝试通过命令行发布我的应用程序时,出现以下错误:

dotnet cake build/build.cake --target=Publish --Verbosity=Diagnostic --runtimeIdentifier=win-x64

结果:

C:\\ProgramFiles\\dotnet\\sdk\\6.0.100\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.Publish.targets(898,5): error MSB4018: The \"GenerateBundle\" task failed unexpectedly. [C:\\FolderHost\\ProjectFileHost.csproj]
C:\\ProgramFiles\\dotnet\\sdk\\6.0.100\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.Publish.targets(898,5): error MSB4018: System.IO.FileNotFoundException: Could not find file \'C:\\FolderHost\\obj\\Release\\net6.0\\win-x64\\singlefilehost.exe\'. [C:\\FolderHost\\ProjectFileHost.csproj]
C:\\ProgramFiles\\dotnet\\sdk\\6.0.100\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.Publish.targets(898,5): error MSB4018: File name: \'C:\\FolderHost\\obj\\Release\\net6.0\\win-x64\\singlefilehost.exe\' [C:\\\\FolderHost\\ProjectFileHost.csproj]
C:\\ProgramFiles\\dotnet\\sdk\\6.0.100\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.Publish.targets(898,5): error MSB4018:    at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite) [C:\\FolderHost\\ProjectFileHost.csproj]
C:\\ProgramFiles\\dotnet\\sdk\\6.0.100\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.Publish.targets(898,5): error MSB4018:    at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite) [C:\\FolderHost\\ProjectFileHost.csproj]
C:\\ProgramFiles\\dotnet\\sdk\\6.0.100\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.Publish.targets(898,5): error MSB4018:    at Microsoft.NET.HostModel.AppHost.BinaryUtils.CopyFile(String sourcePath, String destinationPath) [C:\\FolderHost\\ProjectFileHost.csproj]
C:\\ProgramFiles\\dotnet\\sdk\\6.0.100\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.Publish.targets(898,5): error MSB4018:    at Microsoft.NET.HostModel.Bundle.Bundler.GenerateBundle(IReadOnlyList`1 fileSpecs) [C:\\FolderHost\\ProjectFileHost.csproj]
C:\\ProgramFiles\\dotnet\\sdk\\6.0.100\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.Publish.targets(898,5): error MSB4018:    at Microsoft.NET.Build.Tasks.GenerateBundle.ExecuteCore() [C:\\FolderHost\\ProjectFileHost.csproj]
C:\\ProgramFiles\\dotnet\\sdk\\6.0.100\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.Publish.targets(898,5): error MSB4018:    at Microsoft.NET.Build.Tasks.TaskBase.Execute() [C:\\FolderHost\\ProjectFileHost.csproj]
C:\\ProgramFiles\\dotnet\\sdk\\6.0.100\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.Publish.targets(898,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [C:\\FolderHost\\ProjectFileHost.csproj]
C:\\ProgramFiles\\dotnet\\sdk\\6.0.100\\Sdks\\Microsoft.NET.Sdk\\targets\\Microsoft.NET.Publish.targets(898,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [C:\\FolderHost\\ProjectFileHost.csproj]
An error occurred when executing task \'Publish\'.
Error: .NET CLI: Process returned an error (exit code 1).

ProjectFileHost.csproj 的属性组:

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <OutputType>Exe</OutputType>
    <AssemblyName>xxxxx.xxxx.xxxxxx</AssemblyName>
    <RuntimeIdentifiers>win-x64;linux-x64</RuntimeIdentifiers>
</PropertyGroup>

在 operations.cake 中发布Artifacts 代码:

void PublishArtifact(string projPath, string runtime, string artfDir)
{
  DotNetCorePublishSettings pub = new DotNetCorePublishSettings()
  {
     Framework = \"net6.0\",
     Runtime =  runtime,
     PublishSingleFile = true,
     Configuration =  \"Release\",
     NoBuild = true,
     NoRestore = true,
     OutputDir = artfDir + \"/\" + runtime
  };

  DotNetCorePublish(projPath, pub);
}

如果我们创建PublishSingleFile = false,则发布命令正在工作并在 Artifacts 中创建应用程序和 dll 文件。但是我们需要在一个 .exe 文件中发布应用程序。任何帮助,将不胜感激。

  • 如果将NoBuildNoRestore 设置为false,有什么区别吗?如果您使用最新的 .NET 6 SDK (ATM 6.0.302) 会有什么不同吗
  • 不,它仍然会产生相同的错误。
  • @Subhankar 是否在 C:\\FolderHost\\obj\\Release\\net6.0\\win-x64`? Does publish work on the commandline when you run dotnet 文件夹中手动创建了另一个 exe?

标签: .net runtime .net-6.0 cakebuild


【解决方案1】:

这不是蛋糕错误,而是 msbuild 错误。不存在要部署的文件 C:\FolderHost\obj\Release\net6.0\win-x64\singlefilehost.exe

我的猜测是,由于您在DotNetCorePublishSettings 中设置了NoBuild = true,因此该项目没有事先构建(在所需的配置中)。

您可以设置NoBuild = false, NoRestore = false,也可以在发布前运行DotNet[Core]Build()

另外,请注意DotNetCore*** 别名已被弃用,取而代之的是新的DotNet*** 别名in Cake 2.0.0

【讨论】:

  • 设置 NoBuild = false, NoRestore = false 没有帮助并导致同样的错误。在发布之前,我已经在运行 DotNet[Core]Build() 并且构建工作正常。仍然在那之后 Publish 导致同样的错误。是的,文件C:\FolderHost\obj\Release\net6.0\win-x64\singlefilehost.exe 不存在可部署。我在同一目录中有一个 apphost 文件。
  • @Subhankar 你有我可以看的演示项目吗?
【解决方案2】:

在 ProjectFileHost.csproj 的属性 Group 中将 PublishSingleFile 设置为 true 创建了 singlefilehost.exe

<PublishSingleFile>true</PublishSingleFile>

【讨论】:

    猜你喜欢
    • 2016-11-22
    • 2023-02-08
    • 1970-01-01
    • 2018-12-22
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2015-05-24
    相关资源
    最近更新 更多