Consider you have the an Azure App Service Web App that handles a large amount of traffic and accesses a database, with an architecture similar to that shown in Figure 1. When you make a deployment you would not want to take the risk of deploying bugs or having significant downtime when you deploy a new release, this is what you would want to have a Deployment Slot. 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. The beauty of the deployment slot is that you can slick a button and the deployment slot which contains the new version is swapped with the production, and bang, the new version is live, with just a click.
Notice in Figure 1 there is a SQL Server which has 2 SQL Azure databases on it, one called stickyslot-pro and the other stickyslot-tst. Additionally, there is an App Service Plan (ASP) called STICKYSLOT-ASP that runs an Azure App Service Web App called stickyslot with a deployment slot named testing. All of which are contained within a Resource Group called STICKSLOT-RG.
Figure 1, best case, App Service architecture diagram
Here is a very good article that discusses sticky slots.
In this article I am concerned with the swapping of the Azure App Service Web App deployment slots from PRO (production) to TST (a testing instances). The thing is, 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. In this scenario, it means that when I swap my testing deployment slot, which is, by the way, getting its data from the stickslot-tst database, to production, the then swapped Web App will be pointing to the stickyslot-tst database instead of the stickyslot-pro database. Therefore, I need a way to swap the Web App development slots but keep the database connection string of the current production Web App pointing to production and the testing pointing to the testing database. This is achieved by making the setting ‘sticky to the slot’.
Assume that for my production Web App, I have 2 values I want to remain ‘production’ when I perform a swap. See Figure 2.
Figure 2, sticky slot settings that remain in production even when swapped with a testing slot
Notice there is a App setting with the name STICKSLOT with a Value = ProductionEnvironment and a Connection string called StickySlotConnectionString that contains the production database name, user is and password, for example. Most importantly, notice that the Slot setting check box is selected which means that is remains with this Web App and is not moved when swapped. Notice also that in the testing development slot, see Figure 3, I have the same App Setting and Connection string (Key and Name), but they have different values, also with the Slot setting checkbox checked so it remains on the testing Web App, no way do I want my testing Web App to ever point to the production environment.
Figure 3, sticky slot settings that remain in testing even when swapped with a production slot
I have added an additional App setting, MoveWhenSwapped, which I have not made sticky, I.e. this one will move with the swap. Therefore, when I swap production and test, then that App setting will be in the production Web App configuration and not in the testing one. Let’s see, when everything is all tested and ready for production on the testing environment, navigate to the testing Web App and click the Swap link, as shown in Figure 4.
Figure 4, swap Azure App Service Web App between slots, sticky slots
There is a feature called “swap with preview” which has a nice write here. Basically, this feature let’s you point your testing environment to production just to make sure everything is working as expected before you begin to route the real traffic. Make sure you either complete the swap or roll it back as you wouldn’t want to keep your testing pointing to production for very log. This may not work in all scenarios if there is a database structure change which may break the production environment if deployed and the testing instance doesn’t run on the exiting production database without upgrade. However, give it s shot JIC. Nonetheless, make the swap and then check the production Web App to confirm the App Settings and the Connection string are the expected result. Indeed they are as show in Figure 5.
Figure 5, sticky slots Azure App Service Web App App Settings and Connection strings
The values remained as expected and the MoveWhenSwapped moved from the testing to the production Web App process. Also note that the MoveWhenSwapped moved, which means it no longer exists on the testing Web App. This means, if you want to swap your Web App and want the connection string to remain as is, then mark the setting as sticky by selecting the Slot setting check box and you are all set.
A little deeper we go
Some questions I had:
- How do I access Connection string and App Settings from my code
- What happens if I have a connection string in my web.config file and what happens if I have both
Let’s answer those questions.
How do I access Connection string and App Settings from my code
To access the connection string from either the value configured in the portal or in the web.config you use the following code.
Pass the name of the connection string and the value is returned.
To access the app setting value, use the following.
What happens if I have a connection string in my web.config file and what happens if I have both
Nothing really, as long as the name is unique, then if the value is in the web.config then the it will be found and if the value is in the portal, it also will be found. Watch out that you do not have the same name for any connection string or app setting. If this happens, then, when I tested it, the values configured in the portal were the values accessed by the code. Therefore, if you have a connection string called StickySlotConnectionString configured in both the portal and a web.config, then changes you make to the value in the web.config will be ignored.