Fun with Windows Server Service Bus and AutoFac – Part 2 – AutoFac

In this post we’ll continue on the journey we started in part 1, building a simple pub/sub scenario with Windows Server Service Bus. This part is about configuring subscription and getting subscribers and configuration form a container. AutoFac in this case. Using some extensions and configuration classes we’ll be able to configure our subscribers in a readable fluent way.

You´ll find the code to go along @ GitHub , far from all code will be covered in the post.

Before we get going lets recap.

Recap

Our goal is to have a simple façade to work with for publishing and subscribing,  looking something like this;

The IHandle<T> stuff might feel a bit icky, that could be solved later. A simple action mighty suffice. The configuration in part 1 look like;

Configuration

This has changed a bit since part 1. Now looking something like this;

Configuration have been added,  in form of a delegate or from the container.

RegisterServiceBus registers an Autofac module for all parts needed for the basic scenario. RegisterHandlers then only registers all classes implementing IHandle from the given assembly. With all the handlers registered we then pick what messages we want to ListenFor. When we listen for a message we’re given a configurator back where we can set configuration for subscription, in this case the ReceiveMode . The configuration could later be extended to hold Rules,Filters etc. Last we could add subscriptions in the bootstrapping, this with default configuration, in no other configuration is registered. A look at the implementation, example;

Nothing special, ListenFor<T> is more interesting ;

We add a special AutoFac handler, this internally resolves all handlers for the given type. The reason this is wrapped is to retrieve the handler in a lazy manner. When the handler is registered we need to add it as a subscriber. This could have been resolved later, but to support both Container and delegate handlers we add it as a subscriber. So we get the IServiceBus registration and adds an activating event that resolves the Bus and adds the handler.

Summary

We’ve looked at the parts closest to the Configuration in this post.  A lot more could be added to the configuration as Rules and Filters for example.  The configuration could also be refactored to support the ServiceBus without a Container.

The core parts are a quick refactoring of Brady’s ServiceBusSimplifier. If you would like to use your favorite Service Bus with Windows/Azure Service Bus, you’ll probably find projects like Azure Service Bus Transport for MassTransit.

You’ll find the code @ github so you could play/fork/refactor away!

Enjoy!

Leave a Reply

Close Menu