XamDev
XamDev

Reputation: 3647

Overriding appsettings values in asp.net core

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.

Upvotes: 3

Views: 12021

Answers (3)

Kirk Larkin
Kirk Larkin

Reputation: 93053

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.

Upvotes: 3

Joey Cai
Joey Cai

Reputation: 20067

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();

Upvotes: 1

Simply Ged
Simply Ged

Reputation: 8642

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.

Upvotes: 0

Related Questions