503.0 – Server has been Shutdown

When developing an ASP.NET Core Web application, you MUST NOT call the CreateBuilder more than once! This should only be in the Program.cs file only. Otherwise, you will get this HTTP Error 503.0 - Server has been shutdown. This error might now show up right away but after a few clicks around then app where CreateBuilder is called, will result in this error.

The main issue with this error is calling the CreateBuilder more than once in the application. I used this to get to the Configuration file. Avoid this by using the IConfiguration interface on the Controller. You can also use a static class as well, for the other classes that can’t really inject an interface into. Also the static method is good for JSON calls! See below.

The problem is explained here: ASP.NET Core: 503 Server has been shutdown

This is how to use the Configuration: Configuration in ASP.NET Core | Microsoft Learn

Static Method

public static class AppExtensions
{
public static IConfiguration Configuration;
}

Then in Program.cs add the following line.
AppExtensions.Configuration = builder.Configuration;

Controller Method

For the Controller

public VersionsController(ModelDbContext context, IConfiguration configuration)
{
// Need to ensure that the connection is changed/set in the Constructor!
var id = AppExtensions.Connection;
var connectionString = configuration.GetConnectionString(id);
}

I discovered this method in this Stack Overflow post: c# - How do I access Configuration in any class in ASP.NET Core? - Stack Overflow