8

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.

3
  • 2
    Have you ever tried setting ASPNETCORE_ENVIRONMENT? Commented Feb 13, 2020 at 2:20
  • How and where did you set the environment variable? Commented Feb 13, 2020 at 7:05
  • only use this one. not both lines. .AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true, reloadOnChange: true); Commented Sep 1, 2023 at 13:54

3 Answers 3

3

Please update your below line:

.SetBasePath(env.ContentRootPath)
Sign up to request clarification or add additional context in comments.

1 Comment

How can I set this in my program.cs, its where am doing everything
2

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

1 Comment

I do stuff like this in my program.cs, how can I do that?
2

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

Comments

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.