How to publish environment specific appsettings in .Net core app?

后端 未结 12 2424
刺人心
刺人心 2020-12-04 15:33

I have 3 environment specific appsettings files in my .Net core application

in project.json I have setup publishOptions

相关标签:
12条回答
  • 2020-12-04 15:47

    One possible way would be to run prepublish or postpublic scripts/commands, for example by running an gulp task executing dotnet publish-iis (alternatively use a task in prepublish section of scripts to copy the files to the before publishing.

    Add this to your project.json:

    "scripts": {
      "postpublish": [ "gulp cleanconfig", "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
    }
    

    You can also run a cmd or shell command here. But actually there shouldn't be any reasons why you would want to do this in the first place, just ship all 3 appconfig files, because on i.e. Azure App Service, you can switch the mode depending on the environment variables which is regulated via the Azure Portal and when publishing, the staging and production slots will be just swapped, but the environmental variables stay.

    You shouldn't store secrets within the appsettings.json though (which I assume you doe and the reason you want to remove the files). Instead, use "user secrets" for development and environmental variables to set connection strings etc. for production. Works like a charm, especially with Azure App Services and docker containers.

    0 讨论(0)
  • 2020-12-04 15:47

    You need to actually add the environment variables, according the official tutorial:

    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true)
        // do not forget to add environment variables to your config!
        .AddEnvironmentVariables();
    
    0 讨论(0)
  • 2020-12-04 15:51

    In VS2017 Adding multiple environment

    steps right click project --> Add --> NewItem - select json file - write file name as 'appsettings.staging.json' or 'appsettings.production.json'

    Add appsettings.staging.json file

    Output Looks like

    0 讨论(0)
  • 2020-12-04 15:53

    I recently had to find a solution for this as well and I accomplished it by adding some settings to the .csproj file and a minor change to Program.cs.

    <Project Sdk="Microsoft.NET.Sdk.Web">
        <!-- ... -->
        <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
            <DebugSymbols>true</DebugSymbols>
            <DebugType>full</DebugType>
            <DefineConstants>DEBUG;TRACE</DefineConstants>
            <EnvironmentName>Development</EnvironmentName>
        </PropertyGroup>
        <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
            <DebugType>pdbonly</DebugType>
            <Optimize>true</Optimize>
            <EnvironmentName>Production</EnvironmentName>
        </PropertyGroup>
        <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Stage|AnyCPU'">
            <DebugType>pdbonly</DebugType>
            <Optimize>true</Optimize>
            <EnvironmentName>Staging</EnvironmentName>
        </PropertyGroup>
        <ItemGroup>
            <Content Remove="appsettings.json" />
            <Content Remove="appsettings.*.json" />
        </ItemGroup>
        <ItemGroup>
            <Content Include="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
            <Content Include="appsettings.*.json" Exclude="appsettings.$(EnvironmentName).json" DependentUpon="appsettings.json" CopyToOutputDirectory="Never" />
            <Content Include="appsettings.$(EnvironmentName).json" DependentUpon="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
        </ItemGroup>
    
        <Target Name="RenameAppsettings" AfterTargets="Publish">
            <Move SourceFiles="$(PublishDir)\appsettings.$(EnvironmentName).json" DestinationFiles="$(PublishDir)\appsettings.overrides.json" />
        </Target>
    </Project>
    

    To explain it a little, I added an <EnvironmentName> element for each configuration so it can be used during the build process. I'm using appsettings.{EnvironmentName}.json (i.e. appsettings.Staging.json) just as an "overrides" file so I just have it rename the necessary JSON file during the build process. When you run dotnet publish -c Stage, for example, it will publish the appsettings.Staging.json file into the publish folder and rename it to appsettings.overrides.json. In your Program.cs, you will just need to include the appsettings.overrides.json file as well:

        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.overrides.json", optional: true, reloadOnChange: true)
    

    I hope it helps!

    Side note: I include appsettings.*.json and set it to CopyToOutputDirectory="Never" just so they still show up in Visual Studio when developing. Otherwise, if you only want the current environment's appsettings file to show in VS, just remove that line from the csproj file.

    0 讨论(0)
  • 2020-12-04 15:57

    You can use MSBuild conditions to optionally include files in the compilation output (or published output).

    <ItemGroup Condition="'$(Configuration)'=='Release'">
      <Content Remove="appsettings.Development.json;appsettings.Staging.json" />
      <None Include="appsettings.Development.json;appsettings.Staging.json" />
    </ItemGroup>
    

    The above ignores the Development and Staging appsettings.json file variants when the compilation target configuration is Release.

    0 讨论(0)
  • 2020-12-04 15:57

    In my case I have several appSettings files for several live environments, e.g. appSettings.Env1Live.json, appSettings.Env2Live.json, etc.

    I looked through the article https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/visual-studio-publish-profiles?view=aspnetcore-3.1 and added the following statements in the corresponding publish (pubxml) config files per each environment.

    E.g. for PublishToEnvironment1.pubxml added:

    <ItemGroup>
    <Content Update="appsettings.*Live.json" CopyToPublishDirectory="Never" />
    <Content Update="appsettings.Development.json" CopyToPublishDirectory="Never" />
    <Content Update="appsettings.Env1Live.json" CopyToPublishDirectory="PreserveNewest" /></ItemGroup>
    

    So, in the published folder I have only two necessary files, other appSettings.*Live.json were not published.

    appsettings.json

    appsettings.Env1Live.json

    0 讨论(0)
提交回复
热议问题