johnnys.newsjohnnys.news

by Joachim Leonfellner ✌️ i'm an indie dev in ❤️ with side-projects

Mar 1, 2024629 words in 4 min


Revisited - let‘s publish a WinUI 3 app as a single exe

In this post, I will revisit the topic of publishing a single exe for a WinUI application. I tried to do this in the past and it was quite a challenge - that’s an understatement, it was impossible. You can go through the original post, where I describe my journey Welcome to Hell - Windows Publishing with .NET MAUI.

As being subscribed to almost every GitHub issue of about SingleFile topic, I was surprised when I saw this issue being closed yesterday.

I was like, what? Is it possible now? I had to try it out. And I did. And it worked. And it was easy. And I was happy. And I am writing this post to share this happiness with you.

My original goal didn’t change - I have a WinUI3 app (same would apply to a .NET MAUI windows app) and I want to publish it as a single exe that can be easily distributed and runs any modern Windows machine. I don’t want to deal with any prerequisites, installers, or dependencies. For the sake of simplicity I will also skip the signing part here.

I started by creating a blank WinUI3 app using Visual Studio 2022. First thing I did was to change the WindowsPackageType to None in the project file. This will configure the project for an unpackaged deployment. I have also added an image to the project to verify that resources are bundled correctly - there were some issues with this in the past.

Step 1: Update your NuGet packages

As per the release notes the PublishSingleFile deployment model is supported in the Windows App SDK 1.5.0 (1.5.240227000) release. Be sure to have this NuGet reference or a higher version in your project.

1
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.5.240227000" />

with this change I also had to bump the BuildTools. At the time of this writing, version 10.0.26031-preview worked for me.

1
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.26031-preview" />

Step 2: Publish your app

With the updated references, you can now publish your app. After several tries, I found that the following combination of parameters works for me. I am testing this on a Windows 11 x64 machine.

1
dotnet publish -c Release -p:Platform=x64 -p:PublishSingleFile=true --self-contained true -p:WindowsAppSDKSelfContained=true

These options can of course also be set in the project file directly.

1
2
3
4
5
<PropertyGroup>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained>
</PropertyGroup>

And voila! After about 30 seconds, I had a single exe in the publish folder. I could run it on my machine without any issues. I verified that everything is bundled correctly by running the exe on a plain Windows Sandbox - it worked there as well.

it works

That’s it for now - I just wanted to share this good news with you. There are more publishing options to explore, like trimming, ready-to-run, and signing. Especially trimming would be interesting to investigate because the single exe with everying contained is quite large.

Single exe properties

I will leave that for another post. I hope this will help you to publish your WinUI3 or .NET MAUI Windows app as a single exe. If you have any questions or comments, feel free to reach out. Here is the source code of the sample app I used for this post.

Happy coding!