3

I am a little confused how to handle the local settings in web projects, for our Azure functions this seems very trivial and we are using local.settings.json and then we use the ConfigurationBuilder as below to handle local vs Azure config settings

private static readonly IConfiguration config = new ConfigurationBuilder()
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

private static readonly string cosmosDBEndpointUrl = config["cosmosDBEndpointUrl"];
private static readonly string cosmosDBPrimaryKey = config["cosmosDBPrimaryKey"];
private static readonly string cosmosDBName = config["cosmosDBName"];
private static readonly string cosmosDBCollectionNameRawData = config["cosmosDBCollectionNameRawData"];

This works fine for our functions so that it reads the local settings for local dev and azure settings for whatever env we publish to.

But in my webproject there is no local.settings.json so how do I handle the same scenario there?

I looked at appsettings.json but the formsat seems different.

2
  • As far as I knew, in ASP.NET Core web app, we can save app settings in appsettings.json(such as { "MyConfig": { "ApplicationName": "MyApp", "Version": "1.0.0" } }). Regarding how to read it, please refer to stackoverflow.com/questions/31453495/… Commented Jan 2, 2020 at 5:54
  • In the .Net Framework web app, we can save app settings in web.config with <appSettings> element for <configuration>(such as <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="countoffiles" value="7" /> <add key="logfilelocation" value="abc.txt" /> </appSettings> </configuration>) . Regarding how to read it, we can use System.Configuration.ConfigurationManager.AppSettings["<key name>"]; Commented Jan 2, 2020 at 6:00

2 Answers 2

2

As HariHaran said, in ASP.NET Core web app, we can save app settings in appsettings.json(such as { "MyConfig": { "ApplicationName": "MyApp", "Version": "1.0.0" } }) on local, we can use the following code:

// in Stratup
var config = new ConfigurationBuilder()
     ..SetBasePath(Environment.CurrentDirectory)
     .AddJsonFile("app.settings.json", optional: true, reloadOnChange: true)
     .AddEnvironmentVariables()
     .Build();
in other class
public MyService(IConfiguration configuration){
  var emailConfig = configuration.GetSection("Email");
  var userName = emailConfig["Username"];
}

When we deploy the web app to Azure, we can directly save these settings and connection strings in the Application settings. Then we can read them as environment variables.

include Microsoft.Extensions.Configuration;

namespace SomeNamespace 
{
    public class SomeClass
    {
        private IConfiguration _configuration;

        public SomeClass(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public SomeMethod()
        {
            // retrieve App Service app setting
            var myAppSetting = _configuration["MySetting"];
            // retrieve App Service connection string
            var myConnString = _configuration.GetConnectionString("MyDbConnection");
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

Both are pretty much same. You can have whatever myFile.config.json as you want. All you need to do is just inject it from your Startup class.

var config = new ConfigurationBuilder()
     ..SetBasePath(Environment.CurrentDirectory)
     .AddJsonFile("app.settings.json", optional: true, reloadOnChange: true)
     .AddEnvironmentVariables()
     .Build();

To access the values from the file you can use the IConfiguration class. Eg :

public MyService(IConfiguration configuration){
  var emailConfig = configuration.GetSection("Email");
  var userName = emailConfig["Username"];
}

A even better approach is to have the .json files based on the environment.

4 Comments

But will this automatically read from Azure app settings when I deploy to Azure? It doesn’t look like it
Are you asking will this automactically deploy the json values in appsettings ?
no, I mean when I use azure functions, it will read the file only locally, the file is never deployed, and in azure it will automatically read app settings, will this work the same way?
Yes it will work the same way, cause this line AddEnvironmentVariables() will publish the settings to the cloud

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.