I have previously explained how to properly configure your web deployment projects to replace web.config sections. The example I used was pretty standard – swap out connection strings to support the specific deployment environment. It worked great, right? Well, today this technique complete fell short for me as I was dealing with a custom section.
We recently introduced NServiceBus message sending into our ASP.NET MVC 2 web application. It’s working so well I can hardly believe it. Anyway, we put a two configSections in place for the NServiceBus:
When I attempted to replace these settings, the WDP threw the following build exception:
An error occurred creating the configuration section handler for MsmqTransportConfig: Could not load file or assembly NServiceBus.Core’ or one of its dependencies. The system cannot find the file specified.
I tried a number of workarounds, but I wasn’t able to resolve the issue. In the end, I abandoned the WebConfigReplacement technique and started replacing the entire the web.config file using an ExcludeFromBuild command…
…and a BeforeBuild Copy task…
This approach considerably reduced the number of configuration files which I need to manage. And it works too. I’m not saying you should change your approach, but be aware of the potential issue if you start to implement custom configuration sections.
Note: I’ve read that the WebConfigReplacement technique will only work for types which have been installed in the GAC. I added NServiceBus.dll and NServiceBus.Core.dll to the GAC and that didn’t make much difference. Perhaps NServiceBus.Core dependencies are the culprit? I also read that some folks have implemented their own msbuild task to do a simple text replacement of the configSource property to solve this problem. The configSource property isn’t made available by NServiceBus and I didn’t go ahead and add it.Lazy me. Last thought: I tried many things but flipping the <ValidateWebConfigReplacement> switch to false would fix everything. It didn’t.