How to Create Single File Deployments in .Net 6 / VS2022

How to dotnet build to create single file deployments where all dependencies are bundled into one executable, even the .Net runtime.

By Tim TrottC# ASP.Net MVC • June 2, 2022
How to Create Single File Deployments in .Net 6 / VS2022

Single file deployments in Visual Studio allow developers to package their applications into a single executable file, including all necessary dependencies. This simplifies deployment and distribution, making sharing and running applications across different environments easier without worrying about missing dependencies or complex installation processes.

This guide will show two methods for building projects using single-file deployment. MSBuild will publish your project and compile a single executable file that contains all the required assemblies, including 3rd party dependencies and even the .Net framework runtime libraries. Although the resulting executable is larger than usual, it allows a single file to be deployed or distributed, and no other dependencies are required. It also provides 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

This option is the easiest as long as you don't have any external dependencies. To set up single file deployments, follow these steps.

  1. Right-click on the project to deploy and click Publish.
  2. Select Publish to Folder from the Target and Specific Target selections.
  3. Select a folder location to publish your project
  4. Once the profile is created, click the edit pencil next to Target Runtime.
  5. Set the target framework to net6.0, deployment mode to self-contained, target runtime to win-x64/win-x86 as appropriate.
  6. Click the File publish options and tick the Produce single file checkbox.
  7. Save, then publish
Single File Deployments - Bundle Everything to Single EXE
Single File Deployments - Bundle Everything to Single EXE

Bundle External Dependencies into Single EXE

Suppose you have any external dependencies or references to third-party DLL. In that case, 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 editing the profile XML file.

Publishing for Single File Deployment on Command Line

Depending on whether you want to bundle the .Net runtime libraries, there are two different commands.

Use this command to publish WITH the .Net core runtime (will create a large file)

powershell
dotnet publish -p:PublishSingleFile=true -r win-x64 -c Release --self-contained true -p:IncludeNativeLibrariesForSelfExtract=true

To publish without .NET core runtime, when the application users already have the runtime installed, use this.

powershell
dotnet publish -p:PublishSingleFile=true -r win-x64 -c Release --self-contained false

Editing Publish Settings XML

It is often easier to edit the XML file to continue to build and publish within Visual Studio without dropping to the command line each time.

Open the project .csproj file in the editor (text mode) and locate the PropertyGroup section, usually near the top. Add in the two lines <PublishSingleFile> and <IncludeAllContentForSelfExtract>

xml
<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 usual.

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 will likely be 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 the bundled assemblies will need to be decompressed into memory when launching the application. You should experiment with the option and see what works best for you.

To enable compression on single file deployments, edit the .csproj and add the following EnableCompressionInSingleFile option to the XML.

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>

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

This post has only 1 comment. Why not join the discussion!

New comments for this post are currently closed.