4

I've got an azure app service that I've set up like this:

Azure Service Settings

But when I call IConfiguration.GetConnectionString("db") I get null back.

I've read articles like this https://mderriey.com/2018/08/21/azure-app-service-connection-strings-and-asp-net-core/ which say "it just works", but they're all several years old. I assume something's changed, but what?

Enumerating over all settings in my IConfiguration object I've got no connection strings. I do in development, where my appsettings.development.json has a connectionStrings: { db: "" } defined.

I can see and read the ENV variable: POSTGRESQLCONNSTR_db from within code, and it's value is correct (what I've set via the Azure portal).

Should I expect to be able to do IConfiguration.GetConnectionString("db")? Or am I expected to switch between reading env variables in prod vs dev.

Do I need to include some nuget package to make IConfiguration work under Azure with these ENV variables and their mad prefixes?

My startup.cs basically looks like:


        public Startup(IConfiguration configuration)
        {
            this.Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

Nothing else in there of interest to this question.

0

3 Answers 3

5

I got confused as well, so here it is:

You have two options to specify Connection String locally:

  1. launchSettings.json (environmentVariables section)
  "environmentVariables": {
     "ASPNETCORE_ENVIRONMENT": "Development",
     "SQLAZURECONNSTR_SomeConnectionString": "DefaultEndpointsProtocol=blah"
   }
  1. appSettings.json
  "ConnectionStrings": {
    "SomeConnectionString": "DefaultEndpointsProtocol=blah"
  }

Having either way will allow you to get the connection string setting by calling:

IConfiguration.GetConnectionString("SomeConnectionString")

Function call above will also work when deployed to Azure, as it is using EnvironmentVariables configuration provider to read settings.

Sign up to request clarification or add additional context in comments.

Comments

4

The POSTGRESQLCONNSTR_ prefix isn't supported by the environment variables configuration provider. The docs shows this, in an indirect fashion, where it states that the following prefixes are supported:

  • CUSTOMCONNSTR_
  • MYSQLCONNSTR_
  • SQLAZURECONNSTR_
  • SQLCONNSTR_

It's also apparent in the source code for the provider.

There are a couple of options for working around this:

  1. Change the Type to Custom in the Connection strings section of the Azure portal.
  2. Change to an Application setting of ConectionStrings:db in the Azure portal.

This is being tracked on GitHub: https://github.com/dotnet/runtime/issues/36123.

Comments

1

Instead of getting the config from the interface

IConfiguration.GetConnectionString("db") 

try to get it from

Configuration.GetConnectionString("db")

And in production you have an empty string in production.appsetting.json and add the value in azure(appservice) configuration directly under connectionstrings(this will override the json setting file). And no nugets are needed for reading from appsettings

3 Comments

Yes, thanks, I was simply showing the type of IConfiguration for the purpose of asking a question, otherwise it wouldn't compile. As for your 2nd tip, thats what I've done, that's what the question asks.
sorry, are you saying you need a placeholder connection string key in the production json in order for it to be overridden? Or will (should) it get set regardless. I have no connectionstring keys in my prod json
yes there need to be an placeholder. The value can be what ever it will be replaced ie "SetInAzure"

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.