Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use Slow Cheetah to transform app.config in Azure Worker Role?

I am trying to use Slow Cheetah to switch between a local db connection string and a SQL Azure connection string. This is in a Azure worker role that I am pushing to Azure with TeamCity. When I look through the log file the Slow Cheetah process is running correctly and is producing a transformed app.config but a future build step(that i don't think I can control) is over writing the transformed file with the original app.config.

Has anyone else had any success with this method or can you point me at another method to switch my connection strings. I was pointed towards just using one connection string and editing the hosts file to point at the db I wanted but that seems messy.

like image 980
Jack Woodward Avatar asked Mar 18 '12 00:03

Jack Woodward


2 Answers

When dealing with production and testing/local environments for Azure, a best practice is to store such configuration information in the Service Configuration files instead of web.config. You can create as many Service Configuration files as you want, then select your desired .cscfg file by GUI or by cspack when publishing your solution. By default, the Azure templates in Visual Studio provide two .cscfg files:

  • ServiceConfiguration.Cloud.cscfg
  • ServiceConfiguration.Local.cscfg

You can use these existing files to add two different connection string entries, or create your own. You can store the connection string value in the .cscfg file as shown below:

<ConfigurationSettings>
  <Setting name="DbConnectionString" value="blah" />
</ConfigurationSettings>

Then, you can get the value of the configuration setting entry in your code as shown below:

RoleEnvironment.GetConfigurationSettingValue("DbConnectionString")

Relevant MSDN topics for this scenario below:

  • Configuring Roles for Windows Azure
  • ServiceConfiguration Schema
like image 173
Monochrome Avatar answered Oct 04 '22 21:10

Monochrome


I agree with Monochrome that connection strings should be put in the Service Configuration, however there are situations where you need some configuration changes not related to connection strings that should be applied only when deploying to Azure. In a project of my own, for example, I needed some log4net configuration to change when deployed to Azure.

I found this article that explains how to make SlowCheetah work with a Worker Role project and Windows Azure. You need to make a small change to your Azure project file to copy the transformed configuration file.

<Target Name="CopyWorkerRoleConfigurations" BeforeTargets="AfterPackageComputeService">
    <Copy SourceFiles="..\WorkerRoleName\bin\$(Configuration)\WorkerRoleName.dll.config" DestinationFolder="$(IntermediateOutputPath)WorkerRoleName" OverwriteReadOnlyFiles="true" />
    </Target>
</Project>

You might have to tweak the SourceFiles attribute to fit your directory structure. But that's all there is to it.

like image 24
René Avatar answered Oct 04 '22 21:10

René