I am trying to compress my program to a single executable. I am following instructions specified here: https://dotnetcoretutorials.com/2019/06/20/publishing-a-single-exe-file-in-net-core-3-0/
When I run my code without /p:PublishSingleFile=true, it works but a single file is not generated (as expected). When I add the aforementioned flag I get the following:
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]
I thought it was an environmental problem, so I created a "HelloWorld" console app, and ran the same script there, it worked and generated a single executable.
All projects are compiled under .NETCORE3.0.
Here is what my ZConsole.csproj looks like:
<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>
Thanks for the help!
In order to debug this issue, I found it useful to uncheck the single-file flag in the Publish options, and inspect the libraries in the output directory. In my specific case, I had two assemblies that referenced multiple same COM interop libraries (IWshRuntimeLibrary and others). I ended up playing with the Embed Interop Types and Copy Local flags of the referenced libraries in such a way that whatever conflict there had been, didn't exist anymore..
After I disable CopyToOutputDirectory of NLog.config, publish to single file works fine.
<None Include="NLog.config" />
<!--<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>-->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With