After upgrading to .NET 9, simple C# statement triggers fatal error in Visual Studio 2022 when Control-C is used to exit

Goober McSnidemouth 0 Reputation points
2024-08-20T13:57:40.47+00:00

I'm using Visual Studio 2022 (17.12.0 - preview 1.0)

I noticed that .NET 9 is now available as an option in Visual Studio 2022 so I tried upgrading from .NET 8. My applications continue to run as expected, with one problem: I'm getting a fatal error when halting my console applications using Control-C, or when closing the console using the X close button in the upper right corner.

Here's the error I'm receiving:

Screenshot 2024-08-19 174023

This applies to all of my console applications that make use of the WebBuilder class. The simplest program that will consistently trigger the error is:

internal class Startup
{
    public static async Task Main(string[] args)
    {
        await WebApplication.CreateBuilder().Build().RunAsync();
    }
}

This code was entered into a new console project, in a new solution, where the top line in the project file has been changed to:

<Project Sdk="Microsoft.NET.Sdk.Web">

In addition, launchsettings.json has been deleted to prevent Brave browser from launching when the app is run. The error still occurs even if this file is present; I just deleted it to remove Brave from the equation.

The app itself works fine. It's only the shutdown, when triggered by Control-C or clicking the close button (X) in the console window, that immediately triggers a fatal error with no hints written to the Event Log.

Interestingly, when the app shuts itself down using IHostApplicationLifetime.StopApplication, the shutdown is smooth. There are no errors.

Running the application from the Powershell command line instead of the debugger causes the error to go away. The error only manifests when the debugger is attached.

Reverting the project from .NET 9 back to .NET 8 also causes the error to go away.

Googling this particular error turns up nothing. Any ideas?

Developer technologies Visual Studio Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Goober McSnidemouth 0 Reputation points
    2024-08-27T21:38:10.1833333+00:00

    The problem is due to a bug in the way the debugger interacts with CET. The error goes away when the following line is added to the project file:

    <CetCompat>false</CetCompat>
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.