Environment Based appsettings.json Configuration for publishing in .NET 5
Tags: dotnet, startup
Published: 2021-02-21 18:43:34

#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

```csharp
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;
        }
```

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmifrvi0qiqk9rw0qq8g.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omm5cx49zhgvdngr12pi.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x6f9kqk8y8o017ok45rg.png)

## CSPROJ SETUP

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

![CSPROJ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/orjlywqxgd0hha87b68s.png)

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.

```xml
 <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>
```

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3yip9ap4u1rgwwxv3ab.png)

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.