35

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.

psycho
  • 1,539
  • 4
  • 20
  • 36

2 Answers2

59

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.

Community
  • 1
  • 1
pasul
  • 1,182
  • 2
  • 12
  • 20
  • 1
    Hi. I've tried what you suggested with no joy. I have added my statrtup code to my question – psycho Jul 28 '17 at 10:18
  • Hey, how do you understand that you connection string is not changed, error during site startup? Have you tried to use the app setting instead of connection string ('another way' on my second picture)? You may also try to use some other name from the 'DefaultConnection'. By the way, you appsettings.json is set to be optional, was it done on purpose? – pasul Jul 28 '17 at 10:35
  • I know that it is referring to the wrong connection string becuasue I have logs that tell me the connection to the database is invalid when logging in and also I can see the connection string is incorrect when I go to appsettings.json in Kudu. I have changes the optional parameter to false and still no joy. – psycho Jul 28 '17 at 13:33
  • 1
    The appsettings.json will stay the same, it is correct. The application settings and connection strings you specify in the Azure UI go the environment variables and by placing the AddJsonFile(..), AddEnvironmentVariables() you determine their precedence. So if the names are correct, you should get the value from environment variables. – pasul Jul 28 '17 at 14:36
  • I believe this is to do with the difference between Web Deploy and the "Deploy Azure App Service" in the VSTS release task. Web Deploy will work this way but I am looking for it to work via CICD – psycho Jul 28 '17 at 15:22
  • Then I don't quite understand what the end result you are trying to achieve. Do you want your VSTS release task have a connection string variable and inject the variable's value into appsettings.json and deploy it to Azure with the value already specified in the file? Or you want to specify the value in the Azure Connection strings UI and make it being used by your web application in runtime? – pasul Jul 28 '17 at 15:58
  • I was attempting to just use vsts to release the application using the out of the box "Deploy Azure App Service" task. I was hoping then that the appsettings.json deployed would be overwritten by the Azure GUI's Application Settings values. – psycho Jul 31 '17 at 08:01
  • As I noted before Azure GUI uses environment variables internally. Anyway, if you do everything the right way, you should be able to utilize the connection string / app setting you specify in the Azure GUI. If you do need an appsettings.json's value be overwritten during you VSTS release (before it is being published to Azure) you can use Colin's ALM Corner Build & Release Tools. – pasul Jul 31 '17 at 08:50
  • [Here's the Colin's ALM Corner Build & Release Tools VTST extension](https://marketplace.visualstudio.com/items?itemName=colinsalmcorner.colinsalmcorner-buildtasks) and [tutorial](http://www.colinsalmcorner.com/post/managing-config-for-net-core-web-app-deployments-with-tokenizer-and-replacetokens-tasks). By the way, I successfully used this extension to replace some values in appsettings.json when there was such a need. So, have I answered you question? Do you have more? – pasul Jul 31 '17 at 08:51
  • Remember to add the NuGet package Microsoft.Extensions.Configuration.EnvironmentVariables for the AddEnvironmentVariables() to work. – nivs1978 Nov 12 '18 at 13:39
  • 2
    Seems outdated. .NET Core is already completely different again. – Florian Winter Apr 04 '19 at 13:14
  • Came here to add the thing that was crucial in my case (.NET Core 2.1 web application) which I found in step 2 of this answer: For nested settings in your appsettings.json, make sure the key in the Azure setting uses colons (:) to separate the levels. I've been using periods (.) and then he applicaiton just doesn't see the Environment variable to match the setting and it defaults to the json file – Céryl Wiltink Jun 06 '19 at 13:19
  • 1
    I found this answer very helpful. What worked for me is to use double underscore instead of semicolon. So for example, use "ConnectionStrings__Psql" instead of "ConnectionStrings:Psql" – KTCheek Mar 21 '22 at 01:54
  • I am developing an application using .NET 6 how I use IHostingEnvironment in program.cs file? – Akash Limbani Jul 27 '22 at 14:11
9

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.

psycho
  • 1,539
  • 4
  • 20
  • 36