'Unpackaged WinUI3 app crashes when calling Bootstrap.Initialize()

I have a WinUI3 app that I want to deploy as an unpackaged app, i.e. an app that does not use the MSIX packaging format.

I followed the instructions given here: Instructions for unpackaged C# WinUI 3 apps and this is working well. I can start the exe file created in the bin folder (debug or release) or debug the application using the "unpackaged" profile.

The next thing I tried was referencing the Windows App SDK at runtime in order to use the features it provides. This does not work, i.e. the applications crashes or more precisely just stops.

What I did was following these steps: Advanced tutorial: Build and deploy an unpackaged app that uses the Windows App SDK

In their example they use a console application that has this startup code, you can see the call to Bootstrap.Initialize() in their code:

namespace DynamicDependenciesTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Bootstrap.Initialize(0x00010000); // Call wrapper that enables the Windows App SDK APIs
            Console.WriteLine("Hello World!");

            // Release the DDLM and clean up.
            Bootstrap.Shutdown();
        }
    }
}

In my WinUI3 app, I inserted the call to Bootstrap.Initialize() into the constructor of the App class. However, as soon as the line with the call to Initialize is executed, the application stops with no visible exception. Both of the following implementations show the same behavior.

Version 1:

    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.InitializeComponent();            

        Bootstrap.Initialize(0x00010000); // --> application stops after this line
    }

Version 2:

    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.InitializeComponent();

        try
        {
            int hresult;
            bool flag = Bootstrap.TryInitialize(0x00010000, out hresult); // --> application stops after this line
        }
        catch (Exception e)
        {

        }
    }

Does anyone know possible reasons for this behavior?
The WinUI3 project references the "Microsoft.WindowsAppSDK" Nuget package and I have also installed Windows App SDK (Version 1 stable) on my system.

This is the first part of my project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net5.0-windows10.0.19041</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>Test.UI</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;arm64</Platforms>
    <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <PublishProfile>win10-$(Platform).pubxml</PublishProfile>
    <UseWinUI>true</UseWinUI>
    <ApplicationIcon>Resources\TASKL.ICO</ApplicationIcon>
    <EnableDefaultPageItems>false</EnableDefaultPageItems>
    <EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>
    <WindowsPackageType>None</WindowsPackageType> // unpackaged
  </PropertyGroup>


Solution 1:[1]

You don't need to Bootstrap your app. The <WindowsPackageType>None</WindowsPackageType> is enough. It will do the bootstraping automatically.

Maybe look at https://github.com/microsoft/WindowsAppSDK/discussions/1935 or rather https://github.com/microsoft/WindowsAppSDK/discussions/1933. There exist a bug if you had the "WinUI 3 Controls Gallery" app from the Microsoft Store installed at any time.

Solution 2:[2]

When using Bootstrap.Initialize(0x00010000) please be sure you remove the WindowsPackageType feature from the YourProjectName.csproj file:

<!--<WindowsPackageType>None</WindowsPackageType>-->

Please also be sure that the used version of WindowsAppSDK in your project packages is the same as your runtime version.

powershell -c $(get-appxpackage micro*win*appruntime*).packagefullname

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sneaky Hulk
Solution 2 pshi-ya-chyel