How to Create Single File Deployments in .Net 6 / VS2022How to dotnet build to create single file deployments where all dependencies are bundled into one executable, even the .Net runtime.
This guide will show you two methods for building your projects using single file deployment in which msbuild will publish your project and compile a single executable file which contains all the required assemblies, including 3rd party dependencies and even the .Net framework runtime libraries. Although the resulting executable is quite a bit larger than normal, it allows a single file to be deployed or distributed and no other dependencies are required. It also allows for portable applications so you don't need to install extra libraries, third-party tools or frameworks on each machine it will run on.
If you have no external dependencies on your project the first option works the best, however, if you have external dependencies such as DirectX, Win32 or any time you reference an external DLL, the second option will bundle these as well.
Bundle Everything to Single EXE with Single File Deployments
As long as you don't have any external dependencies this option is the easiest. To set up single file deployments follow these steps.
- Right-click on the project to deploy and click Publish.
- Select Publish to Folder from the Target and Specific Target selections.
- Select a folder location to publish your project
- Once the profile is created, click the edit pencil next to Target Runtime.
- Set the target framework to net6.0, deployment mode to self-contained, target runtime to win-x64/win-x86 as appropriate.
- Click on the File publish options and tick the Produce single file checkbox.
- Save, then publish
Bundle External Dependencies into Single EXE
If you have any external dependencies or references to third-party DLL you need to add additional configuration options which for some reason are not in the publish wizard. We can do this in one of two ways, publishing the project via the command line, or by editing the profile XML file.
Publishing for Single File Deployment on Command Line
There are two different commands depending on if you want to bundle the .Net runtime libraries or not.
Use this command to publish WITH the .Net core runtime (will create a large file)
dotnet publish -p:PublishSingleFile=true -r win-x64 -c Release --self-contained true -p:IncludeNativeLibrariesForSelfExtract=true
To publish without .NET core runtime, for when the application users already have the runtime installed use this.
dotnet publish -p:PublishSingleFile=true -r win-x64 -c Release --self-contained false
Editing Publish Settings XML
It is often easier to simply edit the XML file so you can continue to build and publish within Visual Studio without dropping to the command line each time.
Open up the project .csproj file in the editor (text mode) and locate the PropertyGroup section which is usually near the top. Add in the two lines
and
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<PublishSingleFile>true</PublishSingleFile>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
</PropertyGroup>
Save the file then publish the project as normal.
Enable Compression for Smaller Single File Deployment Bundles
Because the published executable now contains any dependant libraries and the .Net framework runtimes, the file size is likely to be quite large, in the 150MB+ range. To combat this we can enable compression to reduce the size. Enabling compression will create smaller executable files, however, this is at the cost of performance since when launching the application the bundled assemblies will need to be decompressed into memory first. You should experiment with the option and see what works best for you.
To enable compression on single file deployments, simply edit the .csproj and add the following EnableCompressionInSingleFile
option to the XML.
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<PublishSingleFile>true</PublishSingleFile>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
</PropertyGroup>