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.


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

public interface IServiceBus
	void Subscribe(IHandle handler);
	void Publish(T message);

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;

var container = new ContainerBuilder()


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

var container = new ContainerBuilder().
.ListenFor().Configure(c => c.ReceiveMode = ReceiveMode.ReceiveAndDelete)
.Subscribe(x => Console.WriteLine(string.Format("Received (delegate)
'{0}' with message id of {1}", x.Title, x.Id)))

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;

.Where(x => x.GetInterfaces()

Nothing special, ListenFor<T> is more interesting ;

public ISubscriptionConfigurator ListenFor()
	Builder.RegisterCallback(x => x
		.RegistrationsFor(new TypedService(typeof(IServiceBus)))
		.Activating += (s, e) => ((IServiceBus)e.Instance)

	return new SubscriptionConfigurator(this);

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.


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!


Leave a Reply