0

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 1

1

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.
Sign up to request clarification or add additional context in comments.

3 Comments

Do an have an idea of ways to fix it? Thanks
@Serge just select appropriate configuration sources and define the setting. As you already using env variables as one of sources (builder.AddEnvironmentVariables()) and conn string is a sensitive information, it may be a good option for you -> add an env variable with the same name as your setting and set conn string as it's value. If you deploy to Azure, think about using their Key-Value storage. In general, you may even put it into another encrypted configuration file. Look into Data Protection API for more info.
my problem was that the corresponding Environment Variables already existed on the server, but instead of updating it on azure, I tried to update them in the appsetings.json dev and prod, that has any effect on deployed and running in Azure application :") so once I modified it in azure corresponding slots app setting it worked like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.