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": {
    "MyAppContext":     
      "...AccountName=MyApptables4dev; AccountKey=xxxxxx==;EndpointSuffix=core.windows.net"
  },
  "Authentication": {
    "AzureAd": {
      "AADInstance": "https://login.microsoftonline.com/",
      "CallbackPath": "/signin-oidc",
      "ClientId": "xxxxxxx",
      "Domain": "MyAppapp-dev.azurewebsites.net",
      "TenantId": "yyyyyyy"
    }
  }
}

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

"MyAppContext":"...AccountName=MyApptables4dev;AccountKey=yyyyyy==;EndpointSuffix=core..."

and

"Domain": "MyAppapp.azurewebsites.net",

The Startup.cs:

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

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

    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}

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

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

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

{
  "MyAppTablesConnectionString": "DefaultEndpointsProtocol=https;AccountName=myapptables4dev;AccountKey=xxx==;EndpointSuffix=core.windows.net"
}

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:40 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.