- Azure Deployment Slot Connection String Example
- Azure Deployment Slot Connection String Function
- Azure Deployment Slot Connection String Functions
App settings and connection strings for a Web App slot can be marked as a 'slot setting' (i.e., the setting is sticky to the slot). It's not well documented how to specify sticky slot settings in an ARM template. It turns out we can do this using a config resource section called 'slotconfignames' on the production site. 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. 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. EDIT: there's something wonky going on with my connection strings at runtime. When I deploy to a slot, the DB connection string being used to connect to a SQL database for some reason is the one generated in the web.config for the entity framework, not the actual SQL connection string. Since I created the database on the same SQL Azure instance, all I need to do to change my connection string is change the database name. From the Azure Portal, with the staging Deployment Slot active, click on Application settings and modify your connection string as is required for your setup.
This post explains some of the not so well-known features and configurations settings of the Azure App Service deployment slots. These can be used to modify the swap logic as well as to improve the application availability during and after the swap. Here is what you can do with them:
Swap based on the status code
Azure Deployment Slot Connection String Example
During the swap operation the site in the staging slot is warmed up by making an HTTP request to its root directory. More detailed explanation of that process is available at How to warm up Azure Web App during deployment slots swap. By default the swap will proceed as long as the site responds with any status code. However, if you prefer the swap to not proceed if the application fails to warm up then you can configure it by using these app settings:
WEBSITE_SWAP_WARMUP_PING_PATH
: The path to make the warm up request to. Set this to a URL path that begins with a slash as the value. For example, '/warmup.php'. The default value is /.WEBSITE_SWAP_WARMUP_PING_STATUSES
:Expected HTTP response codes for the warm-up operation. Set this to a comma-separated list of HTTP status codes. For example: '200,202' . If the returned status code is not in the list, the swap operation will not complete. By default, all response codes are valid.
You can mark those two app setting as 'Slot Settings' which would make them remain with the slot during the swap. Or you can have them as 'non-sticky' settings meaning that they would move with the site as it gets swapped between slots.
Minimize random cold starts
In some cases after the swap the web app in the production slot may restart later without any action taken by the app owner. This usually happens when the underlying storage infrastructure of Azure App Service undergoes some changes. When that happens the application will restart on all VMs at the same time which may result in a cold start and a high latency of the HTTP requests. While you cannot control the underlying storage events you can minimize the effect they have on your app in the production slot. Set this app setting on every slot of the app:
Azure Deployment Slot Connection String Function
WEBSITE_ADD_SITENAME_BINDINGS_IN_APPHOST_CONFIG
: setting this to '1' will prevent web app's worker process and app domain from recycling when the App Service's storage infrastructure gets reconfigured.
The only side effect this setting has is that it may cause problems when used with some Windows Communication Foundation (WCF) application. If you app does not use WCF then there is no downside of using this setting.
Control SLOT-sticky configuration
Originally when deployment slots functionality was released it did not properly handle some of the common site configuration settings during swap. For example if you configured IP restrictions on the production slot but did not configure that on the staging slot and then performed the swap you would have had the production slot without any IP restrictions configuration, while the staging slot had the IP restrictions enabled. That did not make much sense so the product team has fixed that. Now the following settings always remain with the slot:
- IP Restrictions
- Always On
- Protocol settings (Https Only, TLS version, client certificates)
- Diagnostic Log settings
- CORS
Azure Deployment Slot Connection String Functions
If however for any reason you need to revert to the old behavior of swapping these settings then you can add the app setting WEBSITE_OVERRIDE_PRESERVE_DEFAULT_STICKY_SLOT_SETTINGS
to every slot of the app and set its value to '0' or 'false'.
swap Diagnostics detector
In some cases after the swap the web app in the production slot may restart later without any action taken by the app owner. This usually happens when the underlying storage infrastructure of Azure App Service undergoes some changes. When that happens the application will restart on all VMs at the same time which may result in a cold start and a high latency of the HTTP requests. While you cannot control the underlying storage events you can minimize the effect they have on your app in the production slot. Set this app setting on every slot of the app:
Azure Deployment Slot Connection String Function
WEBSITE_ADD_SITENAME_BINDINGS_IN_APPHOST_CONFIG
: setting this to '1' will prevent web app's worker process and app domain from recycling when the App Service's storage infrastructure gets reconfigured.
The only side effect this setting has is that it may cause problems when used with some Windows Communication Foundation (WCF) application. If you app does not use WCF then there is no downside of using this setting.
Control SLOT-sticky configuration
Originally when deployment slots functionality was released it did not properly handle some of the common site configuration settings during swap. For example if you configured IP restrictions on the production slot but did not configure that on the staging slot and then performed the swap you would have had the production slot without any IP restrictions configuration, while the staging slot had the IP restrictions enabled. That did not make much sense so the product team has fixed that. Now the following settings always remain with the slot:
- IP Restrictions
- Always On
- Protocol settings (Https Only, TLS version, client certificates)
- Diagnostic Log settings
- CORS
Azure Deployment Slot Connection String Functions
If however for any reason you need to revert to the old behavior of swapping these settings then you can add the app setting WEBSITE_OVERRIDE_PRESERVE_DEFAULT_STICKY_SLOT_SETTINGS
to every slot of the app and set its value to '0' or 'false'.
swap Diagnostics detector
If a swap operation did not complete successfully for any reason you can use the diagnostics detector to see what has happened during the swap operation and what caused it to fail. To get to it use the 'Diagnose and solve problems' link in the portal:
From there click on 'Check Swap Operations' which will open a page showing all the swaps performed on the webapp and their results. It will include possible root causes for the failures and recommendations on how to fix them.