Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Shirasagi0012 opened this issue May 26, 2024 · 13 comments
Assignees
Labels
area-ProjectSystem bug Something isn't working team-Reach Issue for the Reach team

Comments

@Shirasagi0012
Copy link

Shirasagi0012 commented May 26, 2024

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 Shirasagi0012 added the bug Something isn't working label May 26, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label May 26, 2024
Copy link

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.

@joelspadin
Copy link

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.

@NixonInnes
Copy link

NixonInnes commented Jun 9, 2024

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.'.

@codendone codendone added area-ProjectSystem team-Reach Issue for the Reach team and removed needs-triage Issue needs to be triaged by the area owners labels Jun 13, 2024
@evelynwu-msft
Copy link
Contributor

evelynwu-msft commented Jun 14, 2024

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.

@IsmailHassani
Copy link

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
Copy link

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"
    }
  }
}

@evelynwu-msft
Copy link
Contributor

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. :)

@ghostidentity
Copy link

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

@IsmailHassani
Copy link

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
Copy link

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"

@ghostidentity
Copy link

@Scottj1s Scottj1s self-assigned this Jul 1, 2024
@Scottj1s
Copy link
Member

Scottj1s commented Jul 3, 2024

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
Copy link
Member

Scottj1s commented Jul 3, 2024

Closing with several options to resolve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-ProjectSystem bug Something isn't working team-Reach Issue for the Reach team
Projects
None yet
Development

No branches or pull requests

8 participants