我正在尝试将程序压缩为单个可执行文件。我正在按照此处指定的说明进行操作:https://dotnetcoretutorials.com/2019/06/20/publishing-a-single-exe-file-in-net-core-3-0/
当我在不使用/ p:PublishSingleFile = true的情况下运行代码时,它可以工作,但未生成单个文件(按预期)。当添加上述标志时,我得到以下信息:
C:\Program Files\dotnet\sdk\3.0.100-preview7-012821\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(801,5): error MSB4018: The "GenerateBundle" task failed unexpectedly. [C:\Users\igorc\Work\ZConsole\ZConsole\ZConsole.csproj]
C:\Program Files\dotnet\sdk\3.0.100-preview7-012821\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(801,5): error MSB4018: System.ArgumentException: Invalid input specification: Found multiple entries with the same BundleRelativePath [C:\Users\igorc\Work\ZConsole\ZConsole\ZConsole.csproj]
C:\Program Files\dotnet\sdk\3.0.100-preview7-012821\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(801,5): error MSB4018: at Microsoft.NET.HostModel.Bundle.Bundler.GenerateBundle(IReadOnlyList`1 fileSpecs) [C:\Users\igorc\Work\ZConsole\ZConsole\ZConsole.csproj]
C:\Program Files\dotnet\sdk\3.0.100-preview7-012821\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(801,5): error MSB4018: at Microsoft.NET.Build.Tasks.GenerateBundle.ExecuteCore() in /_/src/Tasks/Microsoft.NET.Build.Tasks/GenerateBundle.cs:line 36 [C:\Users\igorc\Work\ZConsole\ZConsole\ZConsole.csproj]
C:\Program Files\dotnet\sdk\3.0.100-preview7-012821\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(801,5): error MSB4018: at Microsoft.NET.Build.Tasks.TaskBase.Execute() in /_/src/Tasks/Common/TaskBase.cs:line 47 [C:\Users\igorc\Work\ZConsole\ZConsole\ZConsole.csproj]
C:\Program Files\dotnet\sdk\3.0.100-preview7-012821\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(801,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [C:\Users\igorc\Work\ZConsole\ZConsole\ZConsole.csproj]
C:\Program Files\dotnet\sdk\3.0.100-preview7-012821\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(801,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [C:\Users\igorc\Work\ZConsole\ZConsole\ZConsole.csproj]
我认为这是一个环境问题,因此我创建了一个“ HelloWorld”控制台应用程序,并在其中运行了相同的脚本,它起作用并生成了一个可执行文件。
所有项目均在.NETCORE3.0下编译。
这是我的ZConsole.csproj的样子:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<ApplicationIcon>logo.ico</ApplicationIcon>
<StartupObject>ZConsole.ZConsole</StartupObject>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<TargetFramework>netcoreapp3.0</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="log4net" Version="2.0.8" />
<PackageReference Include="log4net-loggly" Version="9.0.0" />
<PackageReference Include="Nancy" Version="2.0.0" />
<PackageReference Include="Nancy.Hosting.Self" Version="2.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
<PackageReference Include="System.IO.Ports" Version="4.6.0-preview4.19114.5" />
<PackageReference Include="Topshelf" Version="4.2.1" />
<PackageReference Include="WebSocketSharp-NonPreRelease" Version="1.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ZWaveDll\BasicApplication\BasicApplication_netcore.csproj" />
<ProjectReference Include="..\ZWaveDll\Utils\Utils_netcore.csproj" />
<ProjectReference Include="..\ZWaveDll\ZWaveXml\ZWaveXml_netcore.csproj" />
<ProjectReference Include="..\ZWaveDll\ZWave\ZWave_netcore.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="AssemblyInfo.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="App.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
感谢您的帮助!
答案 0 :(得分:2)
为了调试此问题,我发现取消选中“发布”选项中的单文件标志并检查输出目录中的库很有用。在我的特定情况下,我有两个程序集引用了多个相同的COM互操作库(IWshRuntimeLibrary等)。我最终使用了所引用库的Embed Interop Types和Copy Local标志,使之不再存在任何冲突。
答案 1 :(得分:0)
添加
<CopyLocalLockFileAssemblies>false</CopyLocalLockFileAssemblies>
.csproj解决了发布问题
参考 https://github.com/dotnet/runtime/issues/3735#issuecomment-666641134
答案 2 :(得分:0)
禁用 NLog.config 的 CopyToOutputDirectory 后,发布到单个文件工作正常。
<None Include="NLog.config" />
<!--<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>-->