#TIL Modifying Your Azure ApplicationHost.config

For a lot of platform-as-a-service scenarios, you don’t necessarily need to control every aspect of your server’s configuration – in fact, that’s the point of PaaS; you shouldn’t have to worry about such things. In the off chance you do have to make a manual tweak, it can range anywhere from impossible to difficult to do. With Azure, making a change to a server-level file such as ApplicationHost.config is as simple as setting a variable.

I found this neat little trick while looking into HTTP Compression for web services. To change your applicationhost.config file, you simply need to make a XML Document Transform file, put it in a certain folder, and then set a variable on your Azure instance and you’re good to go.

If you’ve messed with web.configs before, particularly modifying the config to contain certain values depending on the build configuration, you’ve probably seen XDT. It consists of an XML Namespace and a few attributes you decorate your configuration file with – these allow you to update an existing .config file by adding, editing, and removing attributes and entire elements. An XDT may look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.applicationHost>
    <sites>
      <site name="%XDT_SCMSITENAME%" xdt:Locator="Match(name)">
        <application path="/somepath" xdt:Locator="Match(path)" 
          xdt:Transform="Remove" />
        <application path="/somepath" xdt:Transform="Insert">
          <virtualDirectory path="/" physicalPath="%XDT_EXTENSIONPATH%" />
        </application>
      </site>
    </sites>
  </system.applicationHost>
</configuration>

You can see the namespace inclusion in the root element, as well as the extra xdt: attributes that allow you to locate and modify elements and attributes, and even use environment variables. This is step 1 – create an XDT file (like above) called ApplicationHost.xdt, and place it in a root-level folder in your web application called SiteExtensions. Once this is set up, publish this file and your website to your Azure instance.

Once you have this done, it’s a simple matter of setting a variable in your Azure web site configuration. On the “Configure” tab, scroll all the way down to the “app settings” section and add the following WEBSITE_PRIVATE_EXTENSIONS variable set to “1”.

You will probably have to restart your website after this change, so save the configuration and hit the Restart button on the same page. Voila! You can see some canned examples of the configuration changes you could make at the Project Kudu github repository.

As a side note, enabling http compression for application/json requests cut down my WebAPI time by 25% – learn more at my Web Service Caching talk next week at Prairie Dev Con!

Comments