0

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

  • appsettings.json
  • appsettings.Local.json

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You gave me the clue which I failed to see here before: learn.microsoft.com/en-us/aspnet/core/test/…

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.