This is part 27 of azure tutorial. In our previous video, we discussed deployment slots, how to
create and their use. In this video, we'll discuss what happens to application configuration settings
such as a database connection string for example when we swap deployment slots in azure. Now,
let's say we have two deployment slots - staging and production. Stating slot points
to the staging database and production slot points to the production database. I have the
exact scenario set up. At the moment, as you can see, we have two deployment slots - production
and staging. What I've done so far is deployed the same version of the app in both the slots.
On the left, we have our production URL. We can say that, because in the URL we don't see the
word "staging" and on the right here we have our staging URL. Again, we can say that because we
see the word "staging" in the URL. Now, our application in the staging deployment slot
is pointing to the staging database and our application in the production deployment slot
is pointing to the production database. If we take a look at the azure portal,
notice we have two databases - ProductionDB and StagingDB. Our production deployment slot
is pointing to the ProductionDB and staging deployment slot to the stagingDB.
Now, both the databases that is production and staging have the same data. So, by looking at
the UI we can't really say the difference. You can't say which version of the app is pointing to
which database. So, if you look at our production version on the left, we have the same five rows
and if we take a look at the staging version on the right, again the same five rows. So, to have
some difference, let's insert a new row into our production database table and I'm going to
do that from sql server management studio. So we have a query here and we are inserting a new row
into our Quotes table in the production database. So, let's execute this query.
Notice, now our production version of the app is displaying the new row that we have just inserted
into the production database whereas our staging version of the app is still displaying
the same old five rows. Now, let's also make a change to our application code. At the moment,
we are displaying the author name just below the quote on the left hand side. Let's change
it to display on the right hand side. The change itself is pretty straightforward. All we have to
do is change the text alignment to right in both the item template and alternating item template.
Now, let's deploy this changed version of our application in the staging deployment slot so
we have the opportunity to test everything and make sure the app works as expected. So,
let's go to the "publish" page and notice in the drop down list here we have our staging
deployment slot selected. Let's publish the changes.
Notice, now on the staging version of the app we have the author name displayed on the right hand
side as expected and we can still say our staging version of the app is pointing to the staging
database because we only see those five rows, we don't see that new row we have just inserted into
the production database and whereas if we take a look at our production version of the app,
we have the author name still displayed on the left hand side and this version of the app is
pointing to the ProductionDB because we see the quote from the production database. Now, let's
say we are happy with our testing and we want to promote this staging build to production. We
do that in azure by swapping the deployment slots staging to production and production to staging.
In the azure portal, we are on the deployment slots page and we see both
our slots - production and staging. We want to swap them. So, let's click this swap button.
Remember we are swapping staging into production and production into staging. So, the source slot
is staging and the target slot is production and take a look at the config changes here. At
the moment, we have source changes selected and our source is our staging slot and here is our
database connection string. When we are swapping these slots the database connection string
is also being swapped. Notice we have old value and new value new value is the one we get after
swap. At the moment, we know our source slot that is our staging slot is pointing to the StagingDB.
We can see that when I hover the mouse over the connection string here. Notice initial catalog
it is pointing to StagingDB. After the swap, you get this new value and notice after swap
our staging slot is pointing to ProductionDB and this is definitely not the behavior we
want and the same is true even for the target slot. Our target slot now is production. At the
moment we know our production slot is pointing to the ProductionDB. Again we can see that from
the connection string here, but after swap our production slot will be pointing to StagingDB.
This is not the behavior we want. Let's actually see this in action. Let's click this swap button.
There we go, swap complete.
Our staging version of the app is successfully promoted to production. We can say that by looking
at our production URL. Notice, now the author name is displayed on the right hand side as expected.
So, our build is definitely promoted to production but then take a look at the data that we have
on our production version of the app, we don't see that new row we have inserted in the production
database. This means our production version of the app is now pointing to the staging database.
This is definitely not the behavior we want. We want the production version of
the app to always be pointing to the production database. Take a look at the staging version
on the right hand side, we see here the quotes from the production database. So,
our staging database is pointing to the production database. So, upon swapping the slots, by default
along with the build our application configuration settings in this case, we have only one setting
and that is the database connection string. This database connection string is also swapped and
this is the reason our production slot is now pointing to the staging database and the staging
slot is pointing to the production database. We don't want this to happen. Even after the swap,
production slot should be pointing to the production database and staging slot
should be pointing to the staging database. So, we basically want the database connection string to
stick with its respective slot. We don't want it to be swappable. This is very easy to achieve. All
we have to do is mark the database connection string setting as a deployment slot setting.
First, let's swap these slots back. So our staging slot will be pointing to the staging database
and the production slot will be pointing to the production database. There we go, swap complete.
Notice, now our production version on the left is pointing to the production database. We can
say that because we can see that quote from the production database and our staging version on
the right hand side is pointing to the staging database. Again, we can say that because we don't
see that row from the production database and the builds are also reverted, because on the staging
version we see the author name is displayed on the right hand side and on our production version, the
author name is displayed on the left hand side. Remember, we don't want our database connection
string to be swappable, we want it to stick with its respective slot. You can very easily
achieve this by marking the database connection string as a deployment slot setting and we do
that on the configuration of our deployment slot. At the moment, we have got two slots.
We can do that either on the production slot or the staging slot. It doesn't really matter. At
the moment we already have our production slot selected. That's why it's not clickable. So,
let's navigate to its configuration. So, notice we have the "Configuration" option
under "Settings". So, let's click on that. We see our application configuration settings here and
let's scroll all the way down. Our database connection string is right here. Notice the name
QuoteDBContext and let's click this "Edit" icon. Remember we want to mark our database connection
string as a deployment slot setting and all we have to do for that is, check this check
box - Deployment slot setting and then click "OK" and very important don't forget to click "Save".
Let's go back to the deployment slots page and for that click on this "Deployment slots" option.
Let's swap these slots. Again, here is the important bit, notice under "Config Changes"
we don't have any changes. This means, our database connection strings are not being swapped.
Let's actually see this in action by swapping these two slots. There we go, swap complete.
Notice, now the author name is displayed on the right hand side on our production version. So,
our staging build is definitely promoted to production and this production version is still
pointing to the production database, we can say that because we see that row
from the production database and on the right we have our staging version. We see the author
name on the left hand side and we don't see that row from the production database. So, this means
our staging version is still pointing to the staging database. So, here's the important
point to keep in mind. If you want to make your application configuration settings
stick with the respective deployment slot then mark all those configuration settings
as deployment slot settings. That's it in this video. Thank you for listening.