[Solved] Minimal Hosting Model: Exited from Program.Main with exit code = ‘0’


The error messages aren’t terribly intuitive, but this error essentially means that the application ran, and then immediately exited. A common cause of this is when you neglect to add the Run() command at the end of the Program:

app.Run();

Background

This is easy to miss if you’re focused on migrating your ConfigureServices() and Configure() methods from your Startup class. While most of your previous configuration code likely lives in that class, the Run() method is one of a few pieces that needs to be migrated from your legacy Program class. You might recognize it from your legacy Main() method; e.g.,

public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();

In the new ASP.NET Core 6 minimal hosting model, this gets translated to:

var builder = WebApplication.CreateBuilder(args);

// Configuration from Startup.ConfigureService()

var app = builder.Build();

// Configuration from Startup.Configure()

app.Run();

When it exits from Program.Main() with exit code 0, that’s telling you that your Program ran correctly—and, thus, seeing logging for your configuration code—but then it stopped without ever actually launching your web application.

Simple fix once you know to look for it.

2

solved Minimal Hosting Model: Exited from Program.Main with exit code = ‘0’