NServiceBus Facility Enhancements

I have added a couple of additions to Ayende’s NServiceBus Facility:

  • Message handlers and sagas are automatically registered with the container for the defined assemblies
  • Message handlers are proxied
  • You can register IHandlerFilter instances with the kernel that allow for interception of messages as they are processed
  • Using my replay strategy implementation, you can decorate a message handler as [Idempotent(Attemps = 5, Delay = 2000)].  That means that a message will be retried when an exception is thrown processing the message 5 times, delaying 2 seconds between each retry.  This could also be extended so that depending on the type of exception that is being thrown, a default retry strategy is applied.

  • The bus is started automatically once all handler dependencies have been satisfied

There were a couple of interesting things that I found when implementing this.  One, NServiceBus retrieives handlers from the kernel using the concrete class instance (looking back Ayende noted this as “yuck”).  That means that we cannot proxy the interface, but instead have to create a class proxy for our message handlers.  That means that when we the message handlers, we have to verify that the message handler methods are virtual and give a nice error if they are not:

Very easy to do with Castle’s fluent interface for registration.  Sagas, on the other hand, have to be registered in the container by each of their interfaces because saga instances are retrieve by calling builder.Build<ISaga<SomeMessage>>().

Out of the box, the way to do message interception with NSB is to have message handlers chained in a specified order.  The IHandlerFilter provides another method for message interception.  If you have a new filter to add, all you have to do is register the IHandlerFilter instance with the container:

The filters follow the chain of responsibility pattern, so they can be short circuited — which is the same as calling bus.DoNotContinueDispatchingCurrentMessageToHandlers().

I have attached the updates to the post: nservicebus-fullduplex-xml-update.  Enjoy.

4 Comments

  1. Tyler Burd:

    Very nice. I was going down this road myself, but you’ve saved me the time! Thanks!

  2. Tyler Burd:

    Quick comment about a minor typo: the subscriptionsQueue is spelled “subsciptionsQueue” (notice the missing ‘r’) in both the facility itself and the config file. I believe this existed in Ayende’s original facility as well.

  3. John:

    I’m getting an XmlException (Invalid character in the given encoding. Line 1, position 6) thrown when attempting to run your project. You didn’t ship a copy of the Utils.dll from nServiceBus, so I used one built on the last stable source release. Is that a known issue in nServiceBus/Windsor.Infrastructure? I re-saved all the configuration files as UTF-8 to eliminate the possibility they were corrupted during decompression.

  4. ehauser:

    John,

    Not sure why you are getting that, I have not had any problems. The NServiceBusAll.dll that I included in the archive had all the necessary ILMerge’d assemblies in it. If you want, you can send me your App.config file and I’ll take a look. ewhauser __at__ gmail.com.

Leave a comment