Wednesday, 22 July 2015

Overcoming assembly binding problems with Web Roles running in Azure Cloud Services

I'd been having trouble getting a Web Role instance started in an Azure Cloud Service - the deployment would run successfully, but the application would constantly recycle.  It turns out that the problem was the Web Role entry point couldn't be created thanks to missing assembly bindings.

It took a lot of digging around, but I managed to find out that the Web Role runs completely independently of the normal website process, meaning it misses out on any assembly binding changes you've made in your web.config file.  According to this blog post from Microsoft:

with full IIS, the RoleEntryPoint runs under WaIISHost.exe, while the web site runs under a normal IIS w3wp.exe process.

The long and the short of this is that you need to include your assembly bindings in a new .config file that WaIISHost.exe will pay attention to.  To do this, do the following:

  • In the web project that forms your web role, create a new .config file
  • The file name should match your project's assembly, such as project.web.dll.config
  • In the file's properties, set Copy to output directory to Copy always

This will put the new .config file in the approot/bin folder when deployed, meaning WaIISHost.exe can read it.  Hooray!

There are lots of related SO questions and blog posts on this: