Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to change the connection string during an Azure VIP Swap

I'm trying to setup Staging and Live environments in Azure (September toolkit) and I want a separate Staging and Live database - with different connection strings. Obviously I can do this with web.config transformations back in Visual Studio, but is there a way I can automate a change of connection string during a VIP-swap - so that the staging site points to staging data and the live site to live data? I'd prefer not to have to deploy twice.

like image 584
Andiih Avatar asked Sep 29 '11 16:09

Andiih


2 Answers

With the management APIs and the PowerShell Cmdlets, you can automate a large amount of the Azure platform and this can include coordinating a VIP switch and a connection string change.

This is the approach:

  1. Add your database connection string to your ServiceConfiguration file.
  2. Modify your app logic to read the connection string from the Azure specific config by using RoleEnvironment.GetConfigurationSettingValue rather than the more typical .NET config ConfigurationManager.ConnectionStrings API
  3. Implement RoleEnvironmentChanging so that your logic will be notified if the Azure service configuration ever changes. Add code to update your app's connection string in here, again using RoleEnvironment.GetConfigurationSettingValue.
  4. Deploy to staging with a ServiceConfiguration setting for your "staging" DB connection string
  5. Write a PowerShell script that will invoke the VIP switch (build around the Move-Deployment cmdlet from the Windows Azure Platform PowerShell Cmdlets 2.0) and invoke a configuration change with a new ServiceConfiguration file that includes your "production" DB connection string (see Set-DeploymentConfiguration)

Taken together, step 5 will perform the VIP switch and perform a connection string update in a single automated operation.

like image 55
Andy Milligan Avatar answered Oct 06 '22 21:10

Andy Milligan


I don't believe anything changes as far as the role is concerned when you do a VIP swap. Rather, it alters the load balancer configuration.

So nothing happens in your app to cause it to change configuration. The only thing I can think of is that the URL changes between the two. You could implement code that chose one of two connection strings, based on the URL with which it was accessed (assuming that we're only talking about a web role), but it seems messy.

Fundamentally, I think the issue is that staging isn't a separate test environment; it's a stepping stone into production. Thus, Microsoft's assumption is that the configuration doesn't change.

like image 44
Steve Morgan Avatar answered Oct 06 '22 21:10

Steve Morgan