Environment Based appsettings.json Configuration for publishing in .NET 5

caiocesar

Caio Cesar

Posted on February 21, 2021

Environment Based appsettings.json Configuration for publishing in .NET 5

Introduction

Sometimes you wish to publish one application to multiple sites. The connection string for applications in .NET are usually located in the appsettings.json. If the release version control is the same for these multiple applications and you wish to configure one appsettings.*.json per client a quick solution would be to create a appsettings file for each client.

Configuration

To start open the program.cs files include the appsettings for each client you wish to configure. This setting will load the appsettings.json



public static IHostBuilder CreateHostBuilder(string[] args)
        {
            var host = Host.CreateDefaultBuilder(args);

            host
                .ConfigureAppConfiguration((hostContext, config) =>
                {
                    config.SetBasePath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
                    config.AddJsonFile("appsettings.json", optional: true, false);
                    config.AddJsonFile("appsettings.client1.json", optional: true);                    
                    config.AddEnvironmentVariables();
                });

            return host;
        }


Enter fullscreen mode Exit fullscreen mode

Make sure the specified json files also exists locally in the application.

Alt Text

Configuration Manager

Setup the configuration manager so that when you publish the application you can choose which configuration you would like to consider for each client.

Alt Text

Click on build > Configuration Manager

Setup a new configuration, by default the debug and release are available, for this example I created a client1 configuration.

Configuration Manager

CSPROJ SETUP

Finally, to configure the correct appsettings.*.json for each environment, open the CSPROJ file.

CSPROJ

For this example we will configure the environment to publish the appsettings.client1.json to the configuration named Client1. Otherwise (for other environments) the appsettings.json will be considered.



<Choose>
<When Condition="'$(Configuration)' == 'Client1'">
<ItemGroup>
<None Include="appsettings.client1.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
<None Include="appsettings.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
<Content Remove="appsettings.*.json;appsettings.json" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<None Include="appsettings.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
<None Include="appsettings.client1.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
</ItemGroup>
</Otherwise>
</Choose>

Enter fullscreen mode Exit fullscreen mode




Testing

To test this configuration I published the application with the Client1 Configuration and Release Configuration to make sure the correct settings are applied to each environment.

The published project configuration should be set to the correct configuration.

Alt Text

After publishing this application the appsettings.client1.json should be the only settings file available.

Final Thoughts

This type of configuration might not be best approach for large production environments, however it will avoid the wrong appsettings configuration on the server for staging or small team projects.

💖 💪 🙅 🚩
caiocesar
Caio Cesar

Posted on February 21, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related