Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Application Settings not overriding my appsettings.json file values

I have tried adding DefaultConnection from my appsettings.json file to Azure's Application Settings but Azure will not override the connection string.

enter image description here

enter image description here

Any article or blog I can find states that all I should need to do is add the connection string name as it states in the appsettings.json file and Azure should do the rest (e.g. https://tehremo.wordpress.com/2016/10/07/override-connection-strings-app-settings-in-asp-net-core-and-azure-app-service/) however when the application is published it is using my local connection string.

My Startup.cs file looks like the following:

enter image description here enter image description here

NOTE: I am publishing using VSTS continuous delivery with "Deploy Azure App Service" release task.

like image 312
psycho Avatar asked Jul 25 '17 09:07

psycho


People also ask

Does Azure app settings override Appsettings json?

For ASP.NET and ASP.NET Core developers, setting app settings in App Service are like setting them in <appSettings> in Web. config or appsettings. json, but the values in App Service override the ones in Web.

How do I get Azure app config connection string?

You can find the connection string under Access Keys in the Azure portal.

How do I set an environment variable in Azure portal?

To set environment variables when you start a container in the Azure portal, specify them in the Advanced page when you create the container. Under Environment variables, enter NumWords with a value of 5 for the first variable, and enter MinLength with a value of 8 for the second variable.


2 Answers

I just had a similar problem (the problem was with PostgreSQL connection string type, I had to change it to custom) and now it works for me, so these are the pieces:

  1. This is my appsettings.json file. I have a value for 'Psql' set in my appsettings.Development.json, but in the appsettings.json it is left empty. enter image description here
  2. These are the settings which are set in the Azure portal. Please note, that there are two ways to override the connection string. enter image description here
  3. This is the part of my Startup.cs file. Pay attention to the order of how the settings are applied in the Startup constructor and the way I get the connection string in the ConfigureServices method (GetConnectionString is a standard extension method). enter image description here

Additional info from my comments below:

Azure GUI (Connection strings, Application settings) uses environment variables internally, so the appsettings.json will stay the same.

If there is a need for an appsettings.json's value to be overwritten during VSTS release activity (before it will be published to Azure), Colin's ALM Corner Build & Release Tools can be used. Here are the links to Colin's ALM Corner Build & Release Tools and tutorial.

like image 143
pasul Avatar answered Oct 08 '22 12:10

pasul


Thanks @pasul, your help was much appreciated and helped me find an alternative solution. In order to deploy using VSTS task and replace application settings, you will need to add variables to the release task and pass into the task the json file in question for variable substitution.

When in "Deploy Azure App Service" release task you should see a "File Transforms and Variable Substitution" section. In here you will supply the path to the json file you want to swap variable values.

enter image description here

Then you will need to click on the options button on the release environment. You will see an option to configure variables in the pop out menu.

enter image description here

From here you can add the json property you want to modify as a variable. In my case the connection string. Which will look like the following:

enter image description here

"ConnectionStrings.DefaultConnection"

Then just put in your connection string value. VSTS will then swap out these values for you when deploying.

like image 38
psycho Avatar answered Oct 08 '22 12:10

psycho