0
votes

Coming from a webforms background, I'm trying to understand how configuration and environment translation works in .net core 2.2 MVC web apps. Gone are the web.config files and the ConfigurationSettings.AppSettings property. I'm finding the documentation a little unclear.

The documentation states I need to call AddJsonFile or AddXmlFile during application startup. Like this:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config.SetBasePath(Directory.GetCurrentDirectory());
            config.AddJsonFile(
                "config.json", optional: true, reloadOnChange: true);
        })
        .UseStartup<Startup>();

The project template I use already has the following logic:

public static void Main(string[] args)
{
    CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>();

My project has appsettings.json and appsettings.development.json files. When I put a breakpoint on the Startup method of the Startup class, I can inspect the configuration parameter and see the two json configuration files exposed as what looks to be a dictionary.

Questions

  1. So do I have to explicitly call AddJSonFile, or is this actually done for me somehow by the framework?

  2. How do I handle transforming configuration for different deployments?

  3. What is the best way to access this configuration in a controller?

1

1 Answers

1
votes

So do I have to explicitly call AddJSonFile, or is this actually done for me somehow by the framework?

This is done in the framework. Most notably the "DefaultBuilder" adds in both appsettings.json and appsettings.{Environment}.json, among other things. https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webhost.createdefaultbuilder?view=aspnetcore-2.2

How do I handle transforming configuration for different deployments?

You need to set the Environment variable on the host machine (This is the easiest way althought here are other ways to do it). So for example if you set the environment to be Production, then it will first load appsettings.json, then it will load appsettings.Production.json and override the default settings. More info here : https://dotnetcoretutorials.com/2017/05/03/environments-asp-net-core/

What is the best way to access this configuration in a controller?

There are two ways. You can use the Options pattern built into the framework : https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options?view=aspnetcore-2.2

Or you can use good old fashioned POCO's (https://dotnetcoretutorials.com/2016/12/26/custom-configuration-sections-asp-net-core/).

All you need to do there is load out your configuration in your ConfigureServices method and bind it to a singleton :

services.AddSingleton(Configuration.GetSection("myConfiguration").Get<MyConfiguration>());

Then you can simply request it in your controller via DI:

public class ValuesController : Controller
{
 private readonly MyConfiguration _myConfiguration;

 public ValuesController(MyConfiguration myConfiguration)
 {
 _myConfiguration = myConfiguration;
 }
}