Search code examples
.net-coreroslyn.net-core-publishsinglefileasp.net-core-5.0

.NET 5 not compiling to single file executables


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


Solution

  • 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