I've got a .NET Framework console app I'm trying to update to .NET 5, but it's failing on certain points that require data from app.config
.
The relevant documentation on how config works now quite helpfully has all of its examples as console apps... right up until you actually look at the examples, which are completely unhelpful. They're all based around a Main
block that looks like this:
static async Task Main(string[] args)
{
using IHost host = CreateHostBuilder(args).Build();
// Application code should start here.
await host.RunAsync();
}
A console app isn't a WinForms app. The application code doesn't "start" in Main; Main
is the main body of your program, which means that putting the line that makes configuration actually work at the end of the method means it will never be hit until the program is ready to terminate!
What am I missing? These documentation examples appear entirely pointless. I'm not trying to host any services; I just want my existing app.config
to be read at startup and made available to the config system, exactly the way it used to work in .NET Framework. What's the proper way to get the old behavior back?