Azure Deployment Slot Connection String

  
  1. Azure Deployment Slot Connection String Example
  2. Azure Deployment Slot Connection String Functions
Example

By default, App Settings and database connection strings are NOT sticky to the slot and will follow the Web App when the test slot is swapped with the production slot. What happens if I have a connection string in my web.config file and as an App Setting, what happens if I have both. Nothing really, as long as the name is unique. A deployment slot is an additional Azure App Service Web App instance (W3WP) which is bound to your production Azure App Service Web App of the same name and runs on the same App Service Plan (ASP) that I discuss here. This development slot lets you deploy you test or non-production ready code for testing prior to the release to the live Web App.

If you’ve deployed a website to Azure using App Services, you know you can override app settings and connection strings in the web.config using environment variables. This is a very handy feature since you can scrap web.config transforms all together if these are the only areas you need to touch.

Since ASP.NET Core does not use web.configs, at least not in a traditional way, how do we override these settings now? Well, pretty much in the exact same way, just with a different format. First let’s look at the new appsettings.json file that will be in your Core app:

{
“Logging”: {
“IncludeScopes”: false,
“LogLevel”: {
“Default”: “Debug”,
“System”: “Information”,
“Microsoft”: “Information”
}
},
“ConnectionStrings”: {
“ConnectionString1”: “localhost-connection-string”
},
“AppSettings”: {
“Environment”: “localhost”,
“NonSlot1”: “localhost1”,
“NonSlot2”: “localhost2”
}
}

In this example, I simple have a Logging section (in there by default), a ConnectionStrings section (this must be named ‘ConnectionStrings’ if you truly want to use it as such), and an AppSettings section (you can name this anything, but I like to keep it familiar).

To override these in your Azure App Service web app, simply navigate to your web app in Azure (or the deployment slot, as in this image), and open up the Application Settings and scroll down to the App Settings and Connection strings sections.

Azure Deployment Slot Connection StringFunctions

Connection Strings

Connection strings are simple. Since Core encourages you to use a section called ConnectionStrings and only put connection strings in it, you can simply reference the connection string name without any other gunk on it.

App Settings

With app settings we have to craft the key name to match the structure of the json file. So:

“AppSettings”: {
“Environment”: “localhost”,
“NonSlot1”: “localhost1”,
“NonSlot2”: “localhost2”
}

Becomes:

  • AppSettings:Environment
  • AppSettings:NonSlot1
  • AppSettings:NotSlot2

You simply add a colon between the parent and child items. A double underscore also works: AppSettings__Environment.

What’s nice about this is, we can now have nested app settings (if you like) and easily reference them using this new structure and naming convention:

“AppSettings”: {

Azure deployment slot connection strings

Azure Deployment Slot Connection String Example

Function

“AwesomeSettings”: {
“Environment”: “awesomehost”,
“NonSlot1”: “awesomehost1”,
“NonSlot2”: “awesomehost2”
}

“CoolSettings”: {
“Environment”: “coolhost”,
“NonSlot1”: “coolhost1”,
“NonSlot2”: “coolhost2”
}

}

Azure Deployment Slot Connection String Functions

This would look like:

  • AppSettings:AwesomeSettings:Environment
  • AppSettings:CoolSettings:Environment

Very cool stuff! For more reading check these posts out: