Configure the "prod" and "dev" of an ASP.NET Core application

I try to set up the "dev" and "prod" environments for my ASP.NET Core 2.0 application, but it does not work... Here is my config:

The appsettings.json content (exactly the same as appsettings.Development.json)

  "Logging": {
    "IncludeScopes": false,
    "LogLevel": { "Default": "Warning" }
  "ConnectionStrings": {
      "...AccountName=MyApptables4dev; AccountKey=xxxxxx==;"
  "Authentication": {
    "AzureAd": {
      "AADInstance": "",
      "CallbackPath": "/signin-oidc",
      "ClientId": "xxxxxxx",
      "Domain": "",
      "TenantId": "yyyyyyy"

for the appsettings.Production.json just the connection string and the domain name changes:



"Domain": "",

The Startup.cs:

public Startup(IHostingEnvironment env)
    var builder = new ConfigurationBuilder()
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

    if (env.IsDevelopment())
        builder = builder.AddUserSecrets<Startup>();

    Configuration = builder.Build();

public void ConfigureServices(IServiceCollection services)
    // Adds services required for using options.

    // Register the IConfiguration instance which "ConnectionStrings" binds against.
    services.AddSingleton<ITableRepositories, TableClientOperationsService>();
    // ...

the secrets.json (located in the Users folder) content

  "MyAppTablesConnectionString": "DefaultEndpointsProtocol=https;AccountName=myapptables4dev;AccountKey=xxx==;"

And finally the Class to access the DB (AzureTables in my case)

public class TableClientOperationsService : ITableRepositories
    // ... 

    public TableClientOperationsService() { }
    public TableClientOperationsService(IOptions<AppSecrets> optionsAccessor)
        tables = new Dictionary<string, CloudTable>();            
        // 'null' in 'prod'!
        connectionString = optionsAccessor.Value.MyAppTablesConnectionString; 

Knowing that MyAppTablesConnectionString is "null" in "Production", maybe the problem comes from there, but I don't know how to make it work with both the MyAppContext and the MyAppTablesConnectionString connection strings...

1 answer

  • answered 2017-10-11 10:07 Set

    So your code read value from MyAppTablesConnectionString setting:

    connectionString = optionsAccessor.Value.MyAppTablesConnectionString; 

    But accordingly to provided info, only dev env has this setting (defined via secret.json and read by builder = builder.AddUserSecrets<Startup>();).

    So yes, production environment got null, as the setting isn't defined in any configuration sources:

    • user secret isn't used with prod
    • appsettings files don't describe MyAppTablesConnectionString
    • and you don't pass this value via env variables.