StackOverflowException after upgrading from .Net 6 to .Net 8

Abhishek, Burra 5 Reputation points
2024-05-29T09:57:50.6666667+00:00

Hi,

I have upgraded 5 projects in my solution from .Net 6 to .Net 8 using upgrade assistant in Visual Studio.

When I run the solution, StackOverflowException is being thrown and Swagger is not loading. Looks like onchangetokenfired() method is being called many times resulting in infinite loop.

PFA Startup.cs.txt and Program.cs.txt

Please help in fixing this issue.

User's image

User's image

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,572 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,344 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jerry Fu - MSFT 571 Reputation points Microsoft Vendor
    2024-05-30T09:17:30.4366667+00:00

    Hi,

    CreateHostBuilder() pattern is the .net5 way. Since .net6, the default template has cancelled the Startup.cs` file and use `WebApplication.CreateBuilder like following:

    var builder = WebApplication.CreateBuilder(args);
    ...
    builder.Services.Add...
    var app = builder.Build();
    ...
    app.UseAuthorization();
    ...
    app.Run();
    

    It is not hard to migrate from the old pattern to new pattern. I also suggest you read this classic article talking about this https://andrewlock.net/exploring-dotnet-6-part-2-comparing-webapplicationbuilder-to-the-generic-host/

    ****************************************************************************** 

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".  Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.