Abp: [Suggestion] Create appsettings.Staging.json and appsettings.Production.json while create new project in cli tool

Created on 25 Feb 2020  路  7Comments  路  Source: abpframework/abp

Suggest creating the Staging and Production environment appsettings.json for the host and dbmigrator project. At current, only created appsettings.json and appsettings.Development.json two files.
Many times, the database connection strings are different between the environments.

discussion enhancement normal

Most helpful comment

@Loongle The migrator project won't read the appsettings.{environment}.json. You need to add some codes to do this. Here is my code (in DbMigratorHostedService.cs):
```C#
public async Task StartAsync(CancellationToken cancellationToken)
{
using (var application = AbpApplicationFactory.Create(options=>
{
// other codes

   // add this
   options.Services.ReplaceConfiguration(BuildConfiguration());
}))
{
    // other codes
}

}

and BuildConfiguration like this:
```C#
private static IConfiguration BuildConfiguration()
{
    var configurationBuilder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json");

    var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
    if (environmentName)
    {
        configurationBuilder.AddJsonFile($"appsettings.{environmentName}.json", true);
    }

    return configurationBuilder
        .AddEnvironmentVariables()
        .Build();
}

All 7 comments

I think the file appsettings.Production.json should never be in any non-production environment. Also never be in any code repository. Staging is fine.

ref: https://docs.microsoft.com/zh-tw/aspnet/core/fundamentals/environments?view=aspnetcore-3.1

@sam0817
You can keep the appsettings.Production.json file empty. My suggestion is just to create the files. If you don't want to use it, you can keep it empty or even delete it. Delete a file is much easier than create a file.

@h82258652
Hi there, i think good architecture or pattern should keep developer from mistakes. When we keep the file there, someday somehow someone junior see the file there and put something into(It makes sense to edit what exist there).

Just like something in domain entity make as internal to prevent unexpected usage.
So there is also no such production related file in official template:

dotnet new webapp
dotnet new webapi
dotnet new mvc

@sam0817 I can't seem to read in migraions appsettings.Development.json content.

My entry Program.cs has been configured:
My entry has been configured:

 ...
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    string path = Directory.GetCurrentDirectory();
                    config.SetBasePath(Directory.GetCurrentDirectory());
                    config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
                    config.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", true, true);

                })
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<DbMigratorHostedService>();
                });

Can you tell me how do that? thankyou

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1
It seems that the.net core method cannot replace the global configuration

@Loongle The migrator project won't read the appsettings.{environment}.json. You need to add some codes to do this. Here is my code (in DbMigratorHostedService.cs):
```C#
public async Task StartAsync(CancellationToken cancellationToken)
{
using (var application = AbpApplicationFactory.Create(options=>
{
// other codes

   // add this
   options.Services.ReplaceConfiguration(BuildConfiguration());
}))
{
    // other codes
}

}

and BuildConfiguration like this:
```C#
private static IConfiguration BuildConfiguration()
{
    var configurationBuilder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json");

    var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
    if (environmentName)
    {
        configurationBuilder.AddJsonFile($"appsettings.{environmentName}.json", true);
    }

    return configurationBuilder
        .AddEnvironmentVariables()
        .Build();
}

@h82258652 Sorry, I made the wrong @. But anyway, you are right.

Was this page helpful?
0 / 5 - 0 ratings