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.

Siavash Rostami
  • 1,883
  • 4
  • 17
  • 31
Matt Douhan
  • 2,053
  • 1
  • 21
  • 40
  • 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 https://stackoverflow.com/questions/31453495/how-to-read-appsettings-values-from-json-file-in-asp-net-core – Jim Xu Jan 02 '20 at 05:54
  • In the .Net Framework web app, we can save app settings in web.config with element for (such as ``` ```) . Regarding how to read it, we can use ```System.Configuration.ConfigurationManager.AppSettings[""];``` – Jim Xu Jan 02 '20 at 06:00

2 Answers2

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.

HariHaran
  • 3,642
  • 2
  • 16
  • 33
  • 2
    But will this automatically read from Azure app settings when I deploy to Azure? It doesn’t look like it – Matt Douhan Jan 02 '20 at 13:02
  • Are you asking will this automactically deploy the json values in appsettings ? – HariHaran Jan 02 '20 at 14:51
  • 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? – Matt Douhan Jan 02 '20 at 14:52
  • Yes it will work the same way, cause this line `AddEnvironmentVariables()` will publish the settings to the cloud – HariHaran Jan 02 '20 at 15:36
1

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");
        }
    }
}
Jim Xu
  • 21,610
  • 2
  • 19
  • 39