Best Practice: Simplified Azure ServiceConfiguration

Posted by Nilay Parikh and last modified on Tue Jun 12, 2018.

Best practices are considered as the most secure and optimised, but they don’t come free. Sometimes the cost is additional development and support efforts, and I am happy to concede the opposite argument that it is not always true.

If ServiceConfiguration.cscfg is not storing any sensitive information, the following simplified best practice would be lightweight and easy to manage. It is just using Visual Studio built-in feature called “Config Transformation”. Follow the simple hack and voilà,

1. Create transformation ServiceConfiguration.cscfg file set

  • Make a copy of ServiceConfiguration.cscfg.
  • Rename copy to ServiceConfiguration.Base.cscfg.
  • For each build configuration (e.g. Staging, Production), create a ServiceConfiguration.<build Config name>.cscfg file.

2. Edit the project

  • In Solution Explorer, right-click the project and click Unload Project. The project is marked (unavailable).
  • In Solution Explorer, right-click the unavailable project and click Edit . The project file opens in the Visual Studio XML Editor.
  • Find the following node
  <ItemGroup>
      <ServiceDefinition Include="ServiceDefinition.csdef" />
      <ServiceConfiguration Include="ServiceConfiguration.cscfg" />
  </ItemGroup>
  • Replace with following
    <ItemGroup>
      <ServiceDefinition Include="ServiceDefinition.csdef" />
      <ServiceConfiguration Include="ServiceConfiguration.cscfg" />
      <None Include="ServiceConfiguration.Base.cscfg">
          <DependentUpon>ServiceConfiguration.cscfg</DependentUpon>
      </None>
      <None Include="ServiceConfiguration.Staging.cscfg">
          <DependentUpon>ServiceConfiguration.cscfg</DependentUpon>
      </None>
      <None Include="ServiceConfiguration.Production.cscfg">
          <DependentUpon>ServiceConfiguration.cscfg</DependentUpon>
      </None>
  </ItemGroup>
  • Add the following at the end of the .ccproj file, just above </Project>
    <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets" />
  <Target Name="BeforeBuild">
      <TransformXml Source="ServiceConfiguration.Base.cscfg" Transform="ServiceConfiguration.$(Configuration).cscfg" Destination="ServiceConfiguration.cscfg" />
  </Target>

3. Save, Close and Reload.

  • Save, and close the project file.
  • In Solution Explorer, right-click the unavailable project and click Reload Project.

For advanced user, you can also add the namespace to your ServiceConfiguration.cscfg or sometimes due to XML Transformation requirement it is essential.

<sc:ServiceConfiguration serviceName="NilayCornerService" osFamily="1" osVersion="*" xmlns:sc="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <sc:Role name="NilaysChef">
    <sc:Instances count="1" />
    <sc:ConfigurationSettings>
      <sc:Setting name="MyConnectionString" value="****************************" />
    </sc:ConfigurationSettings>
  </sc:Role>
</sc:ServiceConfiguration>

If you are looking for some tooling help then worth exploring SlowCheetah - XML Transforms

Small development teams can also adopt the same transformation practices with CI/CD builds. You may also like to refer How to deploy solution to different instance size for Azure environments

Disclaimer

Any views or opinions expressed are solely those of the author and do not represent any other person or organisation. THE ARTICLE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. IN NO EVENT SHALL THE AUTHOR(S) OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY.

References