Azure - transforming the csdef file

I adapted this idea from here:

Basically, we had a situation where we had an app hosted in Azure that was reliant on host headers to switch behavior. Now, host headers are defined within the .csdef as bindings - something like this:

    <Site name="Web">
          <Binding name="HttpIn" endpointName="HttpIn" />
          <Binding name="HttpIn_CO" endpointName="HttpIn_CO" hostHeader="" />
      <InputEndpoint name="HttpIn" protocol="http" port="80" />
      <InputEndpoint name="HttpIn_CO" protocol="http" port="82" />

We also had to deploy this app in more than one country, with both sites behaving the same (with some content changes). As we were deploying the same app twice, with different headers, we had 2 options - either have n Azure projects where n is the number of countries we deploy to; or we transform the csdef to change the bindings depending on cloud configuration.

We chose the second option. The first thing I did was read the article linked at the top of this post *hint*.

The basic steps from this article are:

  1. Add the web config transform runner package to your solution
    PM> Install-Package WebConfigTransformRunner
  2. In Windows Explorer, create new .csdef files for each .cscfg file.
  3.  Manually edit the project file to add the new items as content
  4. Add a build target into the Azure project file
I followed these steps and got it all working. I was then immediately annoyed that I had two empty transform files... I had 3 .cscfg files Cloud, Cloud.AU and Local BUT I only wanted to transform Cloud.AU. So, I modified the build script to this:

  <Target Name="AfterValidateServiceModel" Condition="Exists($(ServiceDefinitionTransform))">
  <Exec Command="&quot;$(TransformRunnerExecutable)&quot; @(TargetServiceDefinition) $(ServiceDefinitionTransform) @(TargetServiceDefinition)" />

All I did, was add a condition to the Target - "Exists($(ServiceDefinitionTransform))" which stops the target running if there's not a transform file for it.

I then deleted my 2 unused transform files and was left with one similar to this:

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition  xmlns="" xmlns:xdt="" xdt:Locator="Condition(@name!='' and @schemaVersion!='')">
  <WebRole name="POMvc.Web" vmsize="Small" xdt:Locator="Match(name)" xdt:Transform="SetAttributes" >
      <Site name="Web" xdt:Locator="Match(name)" xdt:Transform="Replace">
          <Binding name="HttpIn" endpointName="HttpIn" />
          <Binding name="HttpIn_CO" endpointName="HttpIn_CO" hostHeader="" />

Notice that I'm also changing the VM size to reduce cost for the less used deployment :)


  1. Is this still working for you? I can't seem to get it to work, and am wondering if an Azure SDK update or something would have prevented this approach from working any more

    1. As far as I'm aware it does... I'll double check on Monday and get back to you

    2. I double checked this morning and it's still working for me on SDK v2.9

    3. Thanks for checking Rob, I must be doing something wrong. I'm on 2.9 too, so I'll have another look and try again

  2. In case it helps anybody else, I was able to get this working with a similar approach...


Post a Comment

Popular posts from this blog

Full text search in Entity Framework 6 using command interception

Trimming strings in action parameters in ASP.Net Web API

Passing the current ClaimsIdentity to the regenerateIdentity callback