asp.net-core-webapiappsettingsasp.net-core-8

How to read development settings when running in-memory test server?


I have an ASP.NET Core 8 Web API project which contains the following setting files:

Following the instructions here, I also have a test project to run an in-memory test server. So far with only this configuration:

var factory = new WebApplicationFactory<Program>()
            .WithWebHostBuilder(builder =>
            {
                builder.ConfigureServices(services => 
                {
                });

                builder.ConfigureTestServices(services =>
                {
                });

                builder.ConfigureAppConfiguration(config =>
                {
                    config.AddConfiguration(Configuration);
                });
            });

The problem is that when the test project runs the in-memory test server. It is picking up the appsetting.json instead the appsettings.Local.json file from the ASP.NET Core Web API project.

var builder = WebApplication.CreateBuilder(args);

//...some services registration here

builder.Cofiguration["ConnectionsStrings:MyData"] // This reads the value from "appsettings" instead "appsettings.Local"

var app = builder.Build();

How can I configure the test project to so the Program.cs is loaded with the appsettings.Local.json values?


Solution

  • For files to be published, config the webconfig file as this answer. How to use different appsettings.json file for different deployment?

    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Local" />
    

    For selecting locally , change the ASPNETCORE_ENVIRONMENT to Local in launchSettings.json.

    Also , there are configurations to control the selection of appsettings more flexibly.

    builder.ConfigureAppConfiguration((context, config) =>
            {
                config.Sources.Clear();
                config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                config.AddJsonFile("appsettings.Local.json", optional: true, reloadOnChange: true);
                config.AddEnvironmentVariables();
            });
    

    This would make the appsettings.json as default, other files to override, and keep the settings remain the same as configured in appsettings.json if not configured in other files.