Wednesday, November 25, 2009

Configuring NServiceBus In Web Applications

My team recently started using NServiceBus for a new project. It has gone extremely well for the most part. Messaging can be tricky, and a tool like NServiceBus can lower the barrier to entry and gently guides developers towards messaging best practices.

Like any tool, NServiceBus has a learning curve, and there were a few problems along the way. The biggest was an issue with configuring web applications as endpoints. Each web application used the fluent configuration API in the Application_Start event handler like so:


NServiceBus.Configure.With()...


It worked fine in almost all cases. However, when one application was deployed to IIS, the configuration code was throwing inexplicable errors. My team lost about a week trying to figure out what was going on, and eventually started investigating workarounds in order to meet the deadline. Workarounds usually result in their own unique set of problems, which I desperately wanted to avoid. Finally, a long session with reflector revealed the problem:



NServiceBus.Configure.WithWeb()...


The difference between the two methods is subtle. The former tells NServiceBus to scan AppDomain.BaseDirectory for assemblies, while the latter tells NServiceBus to scan AppDomain.DynamicDirectory. The reason this affected one application in particular was because of how that application is deployed. Different versions of the application are deployed in virtual directories under the same web application in IIS, so there was a mismatch between some assembly versions.

The NServiceBus samples demonstrate the use of the WithWeb method. Had we paid more attention to the samples, we might have completely avoided this headache.

2 comments:

  1. Thanks for blogging about this problem. We've added a test to the 'With()' method to see if it's being mistakenly used in a web context and an exception that's raised in that case, so that others (and yourself) don't waste any more time trying to figure this one out.

    ReplyDelete