.NET Core Conditional output of appsettings.json for multiple environments

If you only ever deal with one target machine for your app, life is simple.  In the "real world" you often have to deal with multiple environments - local workstation, dev server, qa, production etc.
With the introduction of .net core, the json-based configuration came on the scene.  Now you can separate your environment-specific settings into different files, eg.  appsettings.json, appsettings.Dev.json etc

Ok, great.  So when I publish to a dev server, if I also have 2 other environment config files, they get published into the build directory as well.  I really don't want the settings related to production published on the dev server.  Yes, I could write a powershell script to delete the "extra" files, or just manually delete them.  Surely there had to be a way to do this from either a publish profile, or the csproj file, since both are just msbuild scripts.  After many hours scouring the interweb, I managed to put this section together that deals just with these files.  This goes into the csproj file.  I tried various shenaigans with a publish profile to no avail, all files were published to the destination.

Here goes.  Note the use of <Choose> 
see msbuild reference here


Think of a Switch in C# with case statements.  Only one of the 'When' blocks should get evaluated based on the build config (which you can set in a publish profile, that much at least works).

The end result?  Only the appsettings.json and the desired target-specific json file deployed to the destination.


  <ItemGroup>
    <None Update="appsettings.json">
      <CopyToPublishDirectory>Always</CopyToPublishDirectory>
    </None>
  </ItemGroup>

  <Choose>
    <When Condition="'$(Configuration)' == 'Local' ">
      <ItemGroup>
        <Content Update="appsettings.Local.json">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Update="appsettings.Dev.json">
          <CopyToOutputDirectory>Never</CopyToOutputDirectory>
        </Content>
        <Content Update="appsettings.Release.json">
          <CopyToOutputDirectory>Never</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    </When>
    <When Condition="'$(Configuration)' == 'Dev'">
      <ItemGroup>
        <Content Update="appsettings.Local.json">
          <CopyToOutputDirectory>Never</CopyToOutputDirectory>
        </Content>
        <Content Update="appsettings.Dev.json">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
        <Content Update="appsettings.Release.json">
          <CopyToOutputDirectory>Never</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    </When>
    <When Condition="'$(Configuration)' == 'Release'">
      <ItemGroup>
        <Content Update="appsettings.Local.json">
          <CopyToOutputDirectory>Never</CopyToOutputDirectory>
        </Content>
        <Content Update="appsettings.Dev.json">
          <CopyToOutputDirectory>Never</CopyToOutputDirectory>
        </Content>
        <Content Update="appsettings.Release.json">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    </When>
  </Choose>

No comments:

Post a Comment

Add appsettings.json to .NET 6 Console App

  When you start a new .NET 6 Console app, you will have little more than what you see here.   If one wants to use the json configuration fi...