When you write a web role requiring application specific configuration (like assembly binding), you may have a hard time trying to figure out which app configuration file should be used and how to get it deployed and used in your role. This issue has been hit by many developers and raised in many blogs & forums:
The key point to remember is that full IIS web role entry point (code implemented in WebRole.cs) is loaded into the WaIISHost.exe process and not w3wp.exe. Therefore, you can't expect settings defined in web.config to be used in you role entry point.
There are 2 solutions to this issue :
- One option consists to use WaIISHost.exe.config – see
Reading config files from RoleEntryPoint and your web site
- The other option consists to create a <webrole name>.dll.config file
These are the steps you can use for the second option in Visual Studio :
- In solution explorer, click on your web role project (WebRole1 in this example) and select Add -> Web Configuration File
- Name the configuration file <webrole name>.dll.config (in this example, WebRole1.dll.config)
- Setup the config file content depending on your needs (assembly redirection…etc)
Right click on the config file, select "Properties" and set "Copy to output directory" to "Copy always" :
- Build the Azure package and deploy it to Azure
Alternatively to the above approach, you can use a "Before build" action to copy the web.config to bin\<webrole name>..dll.config :
copy $(ProjectDir)Web.config $(TargetDir)$(TargetFileName).config
Using the context menu, you'll then need to include in the project the bin\<webrole name>.dll.config. With this approach, you'll be able to set all you settings in a single file (web.config).
With the above steps, you should end up with the web role config file copied to the /bin directory of your webrole (E:\sitesroot\0\bin). In case, it isn't, you may also check that the configuration file is present in the package created (open the cspkg file as a zip file and confirm that the config file is present under the path : WindowsAzure1.cspkg\WebRole1_<ID>.cssx\approot\bin\).
I would like to thank Mr Claus Nielsen for having reported the issue and spending time to troubleshoot it with Microsoft Support…