Jack Smithier
Jack Smithier

Reputation: 101

ASP.NET Core appsettings.json not loading the correct settings

In my Azure I have ENVIRONMENT = Development, but my settings don't load.

public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
       .SetBasePath(Directory.GetCurrentDirectory())
       .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // reloadOnChange Whether the configuration should be reloaded if the file changes.
       .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
       .AddEnvironmentVariables() // Environment Variables override all other, ** THIS SHOULD ALWAYS BE LAST
       .Build();

But it always uses the default settings.

Upvotes: 8

Views: 10647

Answers (3)

Jack Smithier
Jack Smithier

Reputation: 101

In my program.cs I did

public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) // reloadOnChange Whether the configuration should be reloaded if the file changes.
            .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ENVIRONMENT")}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables() // Environment Variables override all other, ** THIS SHOULD ALWAYS BE LAST
            .Build();

than I did

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args) // Sets up the default order in which all the configurations are read
            .UseStartup<Startup>()
            .ConfigureAppConfiguration((c, x) =>
            {

                x.AddConfiguration(Configuration); <-------

            })
            .UseSerilog((h, c) => c.Enrich.FromLogContext().WriteTo.Sentry(s =>
            {

                s.Dsn = new Sentry.Dsn(Configuration.GetSection("Sentry:Dsn").Value);
                s.MinimumEventLevel = Serilog.Events.LogEventLevel.Error;
                s.MinimumBreadcrumbLevel = Serilog.Events.LogEventLevel.Information;

            })).UseSentry(x =>
            {
                x.IncludeRequestPayload = true;
            });

Upvotes: 2

Tony
Tony

Reputation: 20132

This is how I config my startup.cs when I deploy to azure

public Startup(
    IConfiguration configuration,
    IHostingEnvironment hostingEnvironment)
{
    _configuration = configuration;
    _hostingEnvironment = hostingEnvironment;

    var builder = new ConfigurationBuilder();

    if (_hostingEnvironment.IsDevelopment())
    {
        builder.AddUserSecrets<Startup>();
    }
    else
    {
        builder
            .SetBasePath(hostingEnvironment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
    }
}

Update: Try this code in Program.cs

private readonly IHostingEnvironment _hostingEnvironment;
private readonly IConfiguration _configuration;

public Program(
    IConfiguration configuration,
    IHostingEnvironment hostingEnvironment)
{
    _configuration = configuration;
    _hostingEnvironment = hostingEnvironment;

    var builder = new ConfigurationBuilder()
            .SetBasePath(hostingEnvironment.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{hostingEnvironment.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
}

Let me know if you still have any problem

Upvotes: 2

Shweta Lodha
Shweta Lodha

Reputation: 98

Please update your below line:

.SetBasePath(env.ContentRootPath)

Upvotes: 3

Related Questions