Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we use CloudConfigurationManager with asp.net 5 JSON configs?

Asp.net 5 has a new configuration system which utilizes json files for settings. You have to manually select the json file you want loaded as your configuration with the .AddJsonFile(string fileName) method of the new Configuration class.

Meanwhile, in Azure land we've got this nifty CloudConfigurationManager class which is supposed to handle grabbing settings from the Azure Website settings or the active web.config if a Azure setting is not found.

But how are these two things intended to work together? I'm having trouble finding any documentation. I'd like to manage my settings in Azure for production but use the config.json for local debugging. I've had a great deal of trouble finding any examples or documentation.

like image 514
tom.dietrich Avatar asked Jun 01 '15 14:06

tom.dietrich


People also ask

How do I access net 6 config?

var builder = WebApplication. CreateBuilder(args); // Add services to the container. ... ConfigurationManager configuration = builder. Configuration; // allows both to access and to set up the config IWebHostEnvironment environment = builder.

How Configuration works in ASP net Core?

Application configuration in ASP.NET Core is performed using one or more configuration providers. Configuration providers read configuration data from key-value pairs using a variety of configuration sources: Settings files, such as appsettings.

What is IConfiguration in .NET core?

The IConfiguration is an interface for . Net Core 2.0. The IConfiguration interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IConfiguration interface is used to read Settings and Connection Strings from AppSettings. json file.


1 Answers

Well, it turns out that when it comes to using CloudConfigurationManager with asp.net 5, the answer is that you don't, and the boilerplate code already covered it. (Thanks to Scott Hanselman for getting back to me on twitter)

So the standard approach is something like this:

IConfiguration configuration = new Configuration()
  .AddJsonFile("config.json") // adds settings from the config.json file
  .AddEnvironmentVariables(); // adds settings from the Azure WebSite config

The order in which these are called means that settings from the environment variables will over-write settings from the local config. All you have to do to make use of this is make sure the Azure settings will mimic your Json settings- so if your json file looks like

{
  "AppSettings": {
    "ConnectionString": "blahblahblah"
  }
}

You'd want to set up your setting in azure to look like

Key: AppSettings:ConnectionString 
Value: blahblahblah

, and then you can go ahead and use the exact same code you'd use for the local config.

var connectionString = Configuration.Get("AppSettings:ConnectionString");
like image 188
tom.dietrich Avatar answered Sep 21 '22 14:09

tom.dietrich