0

I'm try to test my RESTful service with different env setup.

I specify the environment variable in Program.cs

string environment = "Development";

Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", environment);

The Configure in Startup.cs have the correct env value

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger<Startup> logger)
        {
            logger.LogInformation("Configure Called");
            logger.LogInformation("Environment: " + env.EnvironmentName);    // show Development
        ...
        }

But in ConfigureServices GetConnectionString("Default") get from appsettins.json not appsettings.Development.json

public void ConfigureServices(IServiceCollection services)
        {
            ...

            services.AddDbContext<AppDbContext>(options =>
            {
                options.UseSqlServer(Configuration.GetConnectionString("Default"));    // get appsettings.json not from appsettings.Development.json
            });

            ...
        }

where Configuration is setup in Constructor

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

public IConfiguration Configuration { get; set; }

How should I get the correct configuration file in Startup.cs while keeping the environment value setup in Program.cs

iamshaojin
  • 475
  • 1
  • 7
  • 20

0 Answers0