.NET 5 not compiling to single file executables
Asked Answered
R

2

27

I'm having an issue regarding trying to compile my .NET 5 application to a single file executable while debugging through Visual Studio.

My .csproject file is below.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net50</TargetFramework>
    <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <PublishSingleFile>true</PublishSingleFile>
    <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
    <PlatformTarget>x64</PlatformTarget>
  </PropertyGroup>

</Project>

I have my runtime identifier set to winx64 and publish single file set to true, yet when building I'm left with a bunch of DLLs that my application uses building along-side it (a total of 272 in total). I was wondering - how would I package these DLLs into this application? I had thought that publishing it as a single file executable would do that already.

Picture of DLLs along side EXE

Righteous answered 30/9, 2020 at 8:29 Comment(5)
It seems that you're looking at the build output and not the publish output. Try running ` dotnet publish` The parameter PublishSingleFile should be applied thenAcknowledgment
Is there any way to make publishsinglefile work in the build output?Righteous
As publish does not work in place, it requires some sort of intermediate directory. That is why the publish output must be different from the build output. What does the publish output folder look like in your case after running dotnet publish as J.Loscos suggested?Dube
see github.com/dotnet/designs/blob/main/accepted/2020/single-file/… (a document that describes the design single-file apps in .NET 5.0)Chan
Yikes, it's unfortunate that it doesn't allow for single file executables to be able to work on build. I assume there'd be no hacky way around that?Righteous
B
50

For .NET 5, to get a single runnable executable file when you publish your project, the important properties are:

  • PublishSingleFile
  • SelfContained
  • IncludeAllContentForSelfExtract
  • RuntimeIdentifier

You'll either need to include them in the project file themselves, or specify them on the command line.

Project File:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <!--<OutputType>WinExe</OutputType>--><!--Use this for WPF or Windows Forms apps-->
        <TargetFramework>net5.0</TargetFramework>
        <!--<TargetFramework>net5.0-windows</TargetFramework>--><!--Use this for WPF or Windows Forms apps-->
        <PublishSingleFile>true</PublishSingleFile>
        <SelfContained>true</SelfContained>
        <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
        <RuntimeIdentifier>win-x64</RuntimeIdentifier><!--Specify the appropriate runtime here-->
    </PropertyGroup>

</Project>

CLI:

dotnet publish -r win-x64 --self-contained true -p:PublishSingleFile=true -p:IncludeAllContentForSelfExtract=true

There are other properties worth considering depending on what your needs are, for example:

  • PublishTrimmed
  • PublishReadyToRun

See documentation pages here:

https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file https://github.com/dotnet/designs/blob/main/accepted/2020/single-file/design.md

Broider answered 23/12, 2020 at 1:49 Comment(6)
The CLI version with dotnet doesn't work if you have a COM reference. Then, MSBuild.exe should be used, if the Publish action is done in the terminal.Tollefson
The missing line from my project file was <IncludeAllContentForSelfExtract>. Setting up Publish through VS UI doesn't have IncludeAllContentForSelfExtract parameter, adding the line manually in FolderProfile.pubxml file is the magic stepDaves
Note that IncludeAllContentForSelfExtract makes it extract everything, so it behaves like .NET Core 3.1. This may be undesirable if you're trying to avoid the temp directory getting partially deleted as .NET 5 only extracts native libsGeorgetta
using this console command worked for me while using the publish wizard did not (it worked for me in net 3.1, but not in net 5 anymore)Gemology
All of this options still produce few files. I want make small single EXE for my console prog.Heliotrope
Is there a way to do this automatically in the project file? The reason I ask is that these instructions seem to be saying "accept that the project file outputs a DLL. Open a command prompt and type this dotnet magical incantation to get your .EXE file." Is there a way to just get the project file to produce a .EXE without having to go through the manually run dotnet step?Nervous
I
1

Had a similar problem upgrading from .net core 3.1 to .net 6 for a single file app where the app was exiting as soon as it started up. The resolution was to add the below setting to the publish command

/p:IncludeAllContentForSelfExtract=true
Incipit answered 12/1, 2022 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.