Managing Production and Development Settings in ASP.NET Core.

The reality is that you will need to have, at least, two different configurations: one for production and one for development. Here’s how to automate those conversions.

The reality is that our applications need to be configured differently for our development, staging and production environments. As an example, the logging system you use in your production environment is probably going to be significantly different from the service you use in development (assuming you use one in development at all).

ASP.NET Core provides three tools for managing your application’s configuration. At a course-grained level, you can select your appsettings file and your Startup class based on your environment. At a finer-grained level, you can examine the IHostingEnvironment or check the ASPNETCORE_ENVIRONMENT variable to make decisions in your code based on the current environment. Finally, to manage specifying what environment you’re in, you have profiles.

Managing Profiles
Fundamentally, the configuration process is driven by the value in the ASPNETCORE_ENVIRONMENT environment variable (that’s a mouthful: I’ll just call it the “environment variable” from now on). You can’t set the environment variable’s value from your application’s code (though you can set it through Control Panel | System | Advanced System Settings | Environment Variables, from PowerShell or through the Windows Command Prompt).

You can however, set the value of the variable through a setting in your application’s launchsettings.json file. The launchsettings file organizes settings into profiles with each profile triggered by the way that the application is started.

You can find the launchsettings file nested under the Properties node in Solution Explorer, but it’s probably easier to manage the file through the Properties dialog for your project. The Debug tab provides a UI that lists all the profiles in launchsettings and allows you both to update and create profiles.

In a profile, the key value is what the Debug tab calls Launch (it updates a property called “commandName” in the launchsettings file). The Launch setting controls when a profile is invoked. A profile with its Launch set to “IIS Express” will be the profile that’s used when you start debugging in IIS Express.

Leave a Comment

Your email address will not be published. Required fields are marked *