Sitecore Config Layers and ConfigBuilders

Getting rid of transformations, allow the same build to run in any environment.

It has always been a hassle for me to manage web.config transforms, and slow cheetha transformations for config files. It is annoying to have your build servers do multiple builds to essentially have the same code compile with a different set of variables.

It is solved in Octopus Beploy, by having the build agent replacing tokens within the configuration. However a cleaner option was added in .net 4.7.1 .... tadaaa ConfigBuilders.

And while it opens up a whole set of options, like where to save your configuration and secrets, Azure Storage and similar comes to mind. A handy alternative is to use Environment variables, specially for Sitecore implementations, where Environment variables can be set locally for the application pool in the ApplicationHosts.

Luckily microsoft have an example of this in a preview nuget


Setting up the content builders in web.config..

Add config builders to config sections:
<configSections>
 <section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false"/>
 </configSections>

Add a new section defining your environemnt builder:
<configBuilders>
  <builders>
     <add name="Environment" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment" />
  </builders>
</configBuilders>

Example of the appSettings using the environment builder:
<appSettings configBuilders="Environment">
      <add key="role:define" value="SET env var" />
 <add key="env:define" value="Set env var" />
 <add key="search:define" value="Solr" />
</appSettings>