Search code examples
.net-coreconfiguration-files

.NET Core, Parallel Run of the same application with different configuration possible?


I am trying to get my head around the .netcore way of configuring an application.

Szenario:

I am running the same rest api (application) for client1 and client2 on the same server in the DEV-Environment. We have environment specific configurations as well as client specific configurations.

"Old" Way:

With web.config transformations, I created build configurations for dev-client1 and dev-client2 and set the values accordingly...this all worked fine.

"New" Way:

As the appsettings.json depends on the windows environment variable (one per server) and therefore run into troubles. I could not find a way so far to run the apps in parallel on the same server with different configurations.

I might have missunderstood the new way of configuring an application, therefore any help would be appreciated.


Solution

  • in a typical new .NET Core web app, you will see this code in the Startup.cs:

            var configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json")
                .AddJsonFile($"appsettings.{environment.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables()
                .Build();
    

    all you need to do is add another appsettings file with client-specific settings, like:

                .AddJsonFile($"appsettings.client.json", optional: true)
    

    then have your build script copy in the correct build configuration to appsettings.client.json. the ConfigurationBuilder will pick up the settings in all the sources and compile them into the configuration object.

    also check out the full docs:

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration