3

I am overriding the values which are set in appsettings.json file by configuring them in azure portal. To do so I have made following changes which are working fine except when I debug my code.

Startup.cs

public Startup(IConfiguration configuration, IHostingEnvironment env)
{
    Configuration = configuration;

    IConfigurationBuilder builder = new ConfigurationBuilder();

    builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

    builder.AddEnvironmentVariables();

    Configuration = builder.Build();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    DBContext.ConnectionString = Configuration.GetConnectionString("Connectionstr");
    Constants.AppLogicURI = Configuration["MailUri:LogicAppUri"];
    Constants.BlobStorageKey = Configuration["BlobKey:BlobStorageKey"];
    Constants.BlobStorageConnectionString = Configuration["BlobConnectionString:BlobStorageConnectionString"];
    Constants.BlobUri = Configuration["Uri:BlobUri"];
    Constants.StorageAccount = Configuration["AccountName:StorageAccount"];
}

When I am debugging my code I am not getting any of the values from appsettings.json file due to which application failed to start.

I only get the values if I comment out the line Configuration = builder.Build();

Not sure why I need to do this and how to resolve this issue.

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
XamDev
  • 3,377
  • 12
  • 58
  • 97

3 Answers3

3

The reason your appsettings.json file isn't being read is because you are not calling SetBasePath on your ConfigurationBuilder. This is usually handled for you using Directory.GetCurrentDirectory() in WebHost.CreateDefaultBuilder, which itself is used in Program.cs.

Although you could just call SetBasePath in your Startup constructor, there is no real reason to create your own IConfiguration instance when you can just use the IConfiguration that is being passed in. This instance is configured already to read from both appsettings.json and environment variables, with the environment variables overriding those specified in appsettings.json.

Any settings you specify in the Azure portal's "Application settings" and "Connection strings" sections will override those specified in your appsettings.json file, as they are added as environment variables within the Azure environment.

Kirk Larkin
  • 84,915
  • 16
  • 214
  • 203
  • So you are saying I should not keep anything in startup constrcutor except configuration single line. Also, by doing settings in azure portal it will automatically override those appsettings value. Correct me if I am wrong. Can you plz provide any example/pseudo code for the same ? – XamDev Dec 18 '18 at 11:04
  • 1
    That's correct - all you need in your constructor is `Configuration = configuration;` - this will allow the Azure Portal settings to override the `appsettings.json` values. Can you be more specific about what example code you are looking for? – Kirk Larkin Dec 18 '18 at 11:06
  • I am looking only to override the appsettings value by setting app settings and connection strings in azure portal. As I need to configure those settings on azure as per environment. This is the whole aim about it. – XamDev Dec 18 '18 at 11:14
  • 1
    That is what will happen with this setup as is. There's no extra code needed. – Kirk Larkin Dec 18 '18 at 11:15
1

Change

Configuration["BlobKey:BlobStorageKey"];

To

Configuration.GetSection("BlobKey:BlobStorageKey");

Because the Configuration is Startup.Configuration and you have Build() in Program.cs so you need to comment out Configuration = builder.Build();

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
0

You don't need to build the configuration yourself, the .NET Core code does that for you when you call WebHost.CreateDefaultBuilder(...) in Program.cs.

.NET Core will configure the different providers e.g. Azure vault, JSON files, environment variables, command line etc.

The only think you need in Startup is

Configuration = configuration;

You already have this at the top of the Startup method which is why it works when you comment out the Configuration = builder.build(); code.

You can find out more about the different Providers and the order in which the variables are read in the Configuration documentation.

EDIT

The documentation shows the order of providers that .NET Core automatically adds for you. The last sentence of the section on Providers says:

This sequence of providers is put into place when you initialize a new WebHostBuilder with CreateDefaultBuilder. For more information, see Web Host: Set up a host.

Take a look at the link to CreateDefaultBuilder as that also explains what that method configures for you by default.

Simply Ged
  • 8,250
  • 11
  • 32
  • 40
  • So you mean by commenting that line ONLY it will also override the settings in azure if deployed the web api. – XamDev Dec 18 '18 at 05:46
  • 1
    No. You should remove ALL of the code from `Startup` apart from the first line because .NET Core does the `AddJsonFile` and `AddEnvironmentVariables` (plus a few others including `AzureVault`) for you. Take a look at the documentation link in the answer. Read all of the section that the link points to and it should make more sense. – Simply Ged Dec 18 '18 at 05:58
  • Yeah Still its not clear to me.. it means I need to configuration in Program.cs file instead of Startup.cs ? If this so then how the azure environment variable will be overridden with this ? I am referring to this link https://stackoverflow.com/questions/45298562/azure-application-settings-not-overriding-my-appsettings-json-file-values – XamDev Dec 18 '18 at 07:44