简体   繁体   中英

No .NET Core 2.0 appsettings on a published console app

I'm in the process of producing an automated build process for our team's first .NET Core app. Our process is to produce an artefact package (contains all the different types of apps, database migration scripts, etc.) on our build server, and then in a separate step deploy that package to the various environments that we have (dev, test, demo, training, live, etc.)

In full .NET Framework apps, we use Slow Cheetah to perform config transforms on things like windows services and console apps so that when the app is installed on the target system it has the correct configuration.

Now with the .NET Core 2.0 app we have I'm seeing two problems:

  1. The publish operation (which as far as I can see is the preferred way of getting some sort of artefact package for a single app) doesn't contain any appsettings files (except for ASP.NET Core apps).

  2. If I scripted the copy of the appsettings myself how does the deployed version know which appsettings..json to use?

In fact, I'd rather not deploy anything other than the exact settings for that environment. It seems to me that deploying settings for all environments (or anything other than the exact configuration set for the target environment) is an accident waiting to happen. ( EDIT: To emphasise that I would rather not be merging disparate sources of config at runtime because that would be a mess to debug if things aren't working right trying to figure out exactly which config element actually got applied. ) So, how would I "merge" the environment specific items with the main appsettings.json file?

To make publish operation include appsettings.json file to output you need to add following to your .csproj file:

<ItemGroup>
  <None Include="appsettings.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

It is recommended not to have production settings in your repository for security reasons.

Better option is to provide production configuration to the application where it is deployed to. It can be done through either environment variables or command line arguments if it is deployed as windows service for example.

The code can look as follows:

var configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddEnvironmentVariables()
    .AddCommandLine(args)
    .Build();

So, how would I "merge" the environment specific items with the main appsettings.json file?

Octopus Deploy can modify appsettings.json for you as a part of deployment process https://octopus.com/docs/deploying-applications/deploying-asp.net-core-web-applications/json-configuration-variables-feature . That's actually how we ended up in my team.

Also you can write some custom script that will merge the config on the CI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM