Volodymyr V.
Volodymyr V.

Reputation: 147

How does "Connection strings" work in Azure App Service Configuration?

I have .NET 6 app which is hosted in Azure, it is run on Linux. I just created App Service and publish there my app from Visual studio. This app is connected to MS SQL server in Azure.

At first I put connection string into appsettings.json file. Then I wanted to do that in more true way. In the end I found possibility to set connection string into Configuration inside my App Service. There is even special section in Configuration, called "Connection strings".

So, I placed connection string there, remove it from appsettings.json, republish my app and even restart it then in Azure. It seems, app works fine.

It became interesting to me how this connection string is got into my app. I noticed that into container with my app appeared environment variable SQLAZURECONNSTR_MondayDbContext with my connection string.

I tried to do the same locally on my Windows 10. I set environment variable SQLAZURECONNSTR_MondayDbContext and even SQLAZURECONNSTR__MondayDbContext but app is crashed. I also tried set this through command line argument, but the same, app doesn't see connection string.

How does it work in Azure?

Upvotes: 3

Views: 2951

Answers (1)

Volodymyr V.
Volodymyr V.

Reputation: 147

I figured out with this. It turns out this works only when you set this variable through environment variable. This is because EnvironmentVariablesConfigurationProvider is aware of this and some other prefixes: MYSQLCONNSTR_, SQLAZURECONNSTR_, SQLCONNSTR_, CUSTOMCONNSTR_. If you set any environment variable with this prefix you can get them from Configuration as if they were set to ConnectionStrings section.

Upvotes: 2

Related Questions