microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

WinUI 3 - Blank App, Packaged (WinUI 3 in Desktop) C# project template crashes in Release configuration

Open Shirasagi0012 opened this issue 1 year ago • 11 comments

Describe the bug

The app created by Blank App, Packaged (WinUI 3 in Desktop) C# project template crashes in Release configuration with exception on App.g.i.cs Line 32:

global::Microsoft.UI.Xaml.Application.Start((p) => {
    var context = new global::Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext(global::Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread());
    global::System.Threading.SynchronizationContext.SetSynchronizationContext(context);
    new App();
});

The exceptions:

Unhandled exception at 0x00007FFB31F75175 (Microsoft.ui.xaml.dll) in App1.exe: 0xC000027B: An application-internal exception has occurred (parameters: 0x000001B067EF58C0, 0x0000000000000002).
Unhandled exception at 0x00007FFB994F4470 (combase.dll) in App1.exe: 0xC0000602:  A fail fast exception occurred. Exception handlers will not be invoked and the process will be terminated immediately.
Unhandled exception at 0x00007FFB31F75175 (Microsoft.ui.xaml.dll) in App1.exe: 0xC000027B: An application-internal exception has occurred (parameters: 0x000001B0031031D0, 0x0000000000000002).
Unhandled exception at 0x00007FFB994F4470 (combase.dll) in App1.exe: 0xC0000602:  A fail fast exception occurred. Exception handlers will not be invoked and the process will be terminated immediately.

And in Event Viewer there is a crash event:

Faulting application name: App1.exe, version: 1.0.0.0, time stamp: 0x65f90000
Faulting module name: Microsoft.ui.xaml.dll, version: 3.1.5.0, time stamp: 0x5c2afe3b
Exception code: 0xc000027b
Fault offset: 0x0000000000405175
Faulting process id: 0x20278
Faulting application start time: 0x1DAAF3D091E67FA
Faulting application path: E:\Shirasagi\CSharp\App1\App1\bin\x64\Release\net8.0-windows10.0.19041.0\win-x64\AppX\App1.exe
Faulting module path: C:\Program Files\WindowsApps\Microsoft.WindowsAppRuntime.1.5_5001.119.156.0_x64__8wekyb3d8bbwe\Microsoft.ui.xaml.dll
Report Id: 945b6cf3-6710-47f2-aa22-e8377a178e2e
Faulting package full name: b064c45c-ec11-40e2-b3f2-7d0ca5daf8e5_1.0.0.0_x64__kkjvz6qjy8mej
Faulting package-relative application ID: App

Steps to reproduce the bug

  1. Create a new project using the "Blank App, Packaged (WinUI 3 in Desktop)" C# project template. Both Visual Studio 17.10 and 17.11 Preview 1 are able to reproduce the bug.
  2. Switch the Solution Configuration to "Release".
  3. In the debug properties, check the "Enable native code debugging" option so we can see the exception.
  4. Run "App1(Packaged)" with debuging, and the app crashes.
  5. If you switch the configuration back to Debug, the app runs fine.

Expected behavior

The app should run out-of-the-box in both Release and Debug configurations.

Screenshots

屏幕截图 2024-05-26 152015

NuGet package version

WinUI 3 - Windows App SDK 1.5.3: 1.5.240428000

Windows version

Windows Insider Build (xxxxx)

Additional context

OS build 26120.670 Windows App SDK 1.5.240428000 Microsoft.Windows.SDK.BuildTools 10.0.22621.756

Shirasagi0012 avatar May 26 '24 07:05 Shirasagi0012

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

github-actions[bot] avatar May 26 '24 07:05 github-actions[bot]

I get the same issue. A few of the similar issues linked above seem like they match this, but none of them have a solution.

joelspadin avatar Jun 09 '24 18:06 joelspadin

I have the same issue.

Windows Version: 23H2 22631.3593

Microsoft Visual Studio Community 2022 (64-bit) Version 17.10.1

If you disable Xaml generated main and replace with your own, i.e.:

public static partial class Program
{
    [LibraryImport("Microsoft.ui.xaml.dll")]
    private static partial void XamlCheckProcessRequirements();

    [STAThread]
    public static void Main(string[] args)
    {
        XamlCheckProcessRequirements();
        WinRT.ComWrappersSupport.InitializeComWrappers();

        Application.Start(_ =>
        {
            try
            {
                var context = new DispatcherQueueSynchronizationContext(DispatcherQueue.GetForCurrentThread());
                SynchronizationContext.SetSynchronizationContext(context);
                var app = new App();
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex);
            }
        });
    }
}

You get the following:

The thread 108448 has exited with code 0 (0x0).
The thread 36572 has exited with code 0 (0x0).
onecore\windows\directx\database\helperlibrary\lib\directxdatabasehelper.cpp(527)\directxdatabasehelper.dll!625D1B7F: (caller: 625D6C14) ReturnHr(1) tid(20b74) 80070057 The parameter is incorrect.
onecore\windows\directx\database\helperlibrary\lib\directxdatabasehelper.cpp(527)\directxdatabasehelper.dll!625D1B7F: (caller: 625D6C14) ReturnHr(2) tid(20b74) 80070057 The parameter is incorrect.
Microsoft.ui.xaml.dll!63B57751: 80004005 - Unspecified error
Microsoft.ui.xaml.dll!63B76CC2: 80004005 - Unspecified error
Exception thrown at 0x766C98B2 (KernelBase.dll) in BlankApp.exe: WinRT originate error - 0x802B000A : 'The text associated with this error code could not be found.'.

NixonInnes avatar Jun 09 '24 22:06 NixonInnes

Thanks for the report! For now you can workaround this by editing the .pubxml files so that the PublishTrimmed property is false for the Release configuration.

evelynwu-msft avatar Jun 14 '24 01:06 evelynwu-msft

I also tried several options and builds. The strange thing that unpackaged app in release mode works fine. Only packaged output causes these issues. The same behavior i noticed in .Net Maui (Windows) which caused me to use the win10-x64 runtime with -p:UseRidGraph=true build parameter. In WinUI this trick didn't help, but something is wrong for a while now.

IsmailHassani avatar Jun 19 '24 07:06 IsmailHassani

Thanks for the report! For now you can workaround this by editing the launch profiles so that the PublishTrimmed property is false for the Release configuration.

Do you mean the pubxml files?

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
	<PropertyGroup>
		<Configuration>Release</Configuration>
		<Platform>x64</Platform>
		<PublishProtocol>FileSystem</PublishProtocol>
		<RuntimeIdentifier Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) >= 8">win-x64</RuntimeIdentifier>
		<RuntimeIdentifier Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &lt; 8">win10-x64</RuntimeIdentifier>
		<PublishDir>bin\$(Platform)\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
		<SelfContained>true</SelfContained>
		<PublishSingleFile>false</PublishSingleFile>
		<PublishReadyToRun>true</PublishReadyToRun>
		<PublishTrimmed>false</PublishTrimmed>
	</PropertyGroup>
</Project>

LaunchSettings:

{
  "profiles": {
    "ISynergy.App.Commodities (Package)": {
      "commandName": "MsixPackage"
    },
    "ISynergy.App.Commodities (Unpackaged)": {
      "commandName": "Project"
    }
  }
}

IsmailHassani avatar Jun 19 '24 07:06 IsmailHassani

Thanks for the report! For now you can workaround this by editing the launch profiles so that the PublishTrimmed property is false for the Release configuration.

Do you mean the pubxml files?

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
	<PropertyGroup>
		<Configuration>Release</Configuration>
		<Platform>x64</Platform>
		<PublishProtocol>FileSystem</PublishProtocol>
		<RuntimeIdentifier Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) >= 8">win-x64</RuntimeIdentifier>
		<RuntimeIdentifier Condition="$([MSBuild]::GetTargetFrameworkVersion('$(TargetFramework)')) &lt; 8">win10-x64</RuntimeIdentifier>
		<PublishDir>bin\$(Platform)\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
		<SelfContained>true</SelfContained>
		<PublishSingleFile>false</PublishSingleFile>
		<PublishReadyToRun>true</PublishReadyToRun>
		<PublishTrimmed>false</PublishTrimmed>
	</PropertyGroup>
</Project>

LaunchSettings:

{
  "profiles": {
    "ISynergy.App.Commodities (Package)": {
      "commandName": "MsixPackage"
    },
    "ISynergy.App.Commodities (Unpackaged)": {
      "commandName": "Project"
    }
  }
}

Apologies, yes, I meant the pubxml. I've updated my original comment. :)

evelynwu-msft avatar Jun 19 '24 19:06 evelynwu-msft

@evelynwu-msft thank you, I confirmed that the workaround is working.

ghostidentity avatar Jun 19 '24 20:06 ghostidentity

I don't know what I'm doing differently. Let me explain first the setup i'm using:

  • Start Windows Sandbox
  • Run the following scripts with elevated permissions to install winget
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
$progressPreference = 'silentlyContinue'
Write-Information "Downloading WinGet and its dependencies..."
Invoke-WebRequest -Uri https://aka.ms/getwinget -OutFile Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle
Invoke-WebRequest -Uri https://aka.ms/Microsoft.VCLibs.x64.14.00.Desktop.appx -OutFile Microsoft.VCLibs.x64.14.00.Desktop.appx
Invoke-WebRequest -Uri https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.x64.appx -OutFile Microsoft.UI.Xaml.2.8.x64.appx
Add-AppxPackage Microsoft.VCLibs.x64.14.00.Desktop.appx
Add-AppxPackage Microsoft.UI.Xaml.2.8.x64.appx
Add-AppxPackage Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle

On my development machine i'm running the following commands and copy the output to Windows Sandbox and install the msix.

Fails

dotnet publish -c Release -p:Platform=x64 --runtime win-x64 /p:PublishProfile=Properties\PublishProfiles\win-x64.pubxml -p:AppxPackageSigningEnabled=true -p:PackagecertificateThumbprint=xxxxx -p:GenerateAppxPackageOnBuild=true -p:WindowsPackageType=MSIX -p:AppxPackageDir=C:\Temp\
dotnet msbuild /p:Configuration=Release /p:Platform=x64 /p:PublishProfile=Properties\PublishProfiles\win-x64.pubxml /p:AppxBundle=Never /p:GenerateAppxPackageOnBuild=true /p:AppxPackageSigningEnabled=true /p:PackagecertificateThumbprint=xxxxx /p:AppxPackageOutput="C:\Temp\Test_x64.msix"
dotnet publish -c Release -p:Platform=x64 --runtime win-x64 -p:PublishTrimmed=false -p:AppxPackageSigningEnabled=true -p:PackagecertificateThumbprint=xxxxx -p:GenerateAppxPackageOnBuild=true -p:WindowsPackageType=MSIX -p:AppxPackageDir=C:\Temp\
dotnet publish -c Release -p:Platform=x64 --runtime win-x64 -p:PublishTrimmed=false -p:PublishReadyToRun=true -p:AppxPackageSigningEnabled=true -p:PackagecertificateThumbprint=xxxxx -p:GenerateAppxPackageOnBuild=true -p:WindowsPackageType=MSIX -p:AppxPackageDir=C:\Temp\
dotnet publish -c Release -p:Platform=x64 --runtime win-x64 -p:PublishTrimmed=false -p:PublishReadyToRun=true -p:PublishSingleFile=false -p:AppxPackageSigningEnabled=true -p:PackagecertificateThumbprint=xxxxx -p:GenerateAppxPackageOnBuild=true -p:WindowsPackageType=MSIX -p:AppxPackageDir=C:\Temp\
dotnet publish -c Release -p:Platform=x64 --runtime win-x64 -p:PublishTrimmed=false -p:PublishReadyToRun=true -p:SelfContained=true -p:AppxPackageSigningEnabled=true -p:PackagecertificateThumbprint=xxxxx -p:GenerateAppxPackageOnBuild=true -p:WindowsPackageType=MSIX -p:AppxPackageDir=C:\Temp\
dotnet build -c Release -p:Platform=x64
dotnet build -c Release -p:Platform=x64 -p:WindowsPackageType=MSIX

Fails (trick that i use with .Net Maui build for Windows, regular win-x64 doesn't work there either)

dotnet publish -f net8.0-windows10.0.22621.0 -c Release -p:Platform=x64 --runtime win10-x64 -p:UseRidGraph=true -p:AppxPackageSigningEnabled=true -p:PackagecertificateThumbprint=XXXXX -p:GenerateAppxPackageOnBuild=true -p:WindowsPackageType=MSIX -p:AppxPackageDir=C:\Temp\

Succeeds (Sort of. It's runnable)

dotnet build -c Release -p:Platform=x64 -p:WindowsPackageType=None

This is the unpackaged version, but i cannot use it for store submission.

IsmailHassani avatar Jun 21 '24 00:06 IsmailHassani

Btw also the MSbuild commands doesn't work.

msbuild /p:Configuration=Release /p:Platform=x64 /p:AppxBundle=Never /p:GenerateAppxPackageOnBuild=true /p:AppxPackageSigningEnabled=true /p:PackagecertificateThumbprint=XXXXX /p:AppxPackageOutput="C:\Temp\Test_x64.msix"
msbuild /p:Configuration=Release /p:Platform=x64 /p:Runtimeidentifier=win-x64 /p:PublishTrimmed=false /p:PublishReadyToRun=true /p:AppxBundle=Never /p:GenerateAppxPackageOnBuild=true /p:AppxPackageSigningEnabled=true /p:PackagecertificateThumbprint=XXXXXX /p:AppxPackageOutput="C:\Temp\Test_x64.msix"

IsmailHassani avatar Jun 21 '24 00:06 IsmailHassani

@IsmailHassani https://drive.google.com/file/d/1XTKQTb6-pbjYkf6yX8FKS00ZpTwgy1ze/view?usp=sharing you can try the steps.

ghostidentity avatar Jun 22 '24 09:06 ghostidentity

Another strategy, if full trimming is desired, is to update to Windows App SDK 1.6 (currently in experimental release) and also include a C#/WinRT package reference. Windows App SDK 1.6 C# projection assemblies now have support for trimming, and the latest C#/WinRT provides source generation to also support trimming.

Scottj1s avatar Jul 03 '24 18:07 Scottj1s

Closing with several options to resolve

Scottj1s avatar Jul 03 '24 19:07 Scottj1s

Not working for me still by adding the following and several other variations:

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
   <OutputPath>bin\Release\x64\</OutputPath>
   <PublishTrimmed>true</PublishTrimmed>
   <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
 </PropertyGroup>


keithnicholson avatar Aug 15 '24 02:08 keithnicholson

@keithnicholson Sorry that isn't working for you. Could you please open a new issue with a sample so we can investigate? Thanks.

codendone avatar Aug 15 '24 16:08 codendone

same here I tried all above suggestions, no success

fullstackdevsalmoh avatar Sep 02 '25 18:09 fullstackdevsalmoh