Shuttle.Esb

A highly configurable free open-source enterprise service bus that provides you with a mechanism to create Autonomous Business Components that are loosely coupled. This enables you to develop and deploy specific business functionality that can be independently versioned.

Let’s get going!

Start a new Class Library project and select a Shuttle.Esb queue implementation from the supported queues:

Install-Package Shuttle.Esb.Msmq

Now we’ll need select one of the supported containers:

Install-Package Shuttle.Core.Autofac

We’ll also need to host our endpoint within the generic host:

Install-Package Shuttle.Core.Host

Next we’ll implement our endpoint in order to start listening on our queue:

public class Host : IHost, IDisposable
{
	private IServiceBus _bus;

	public void Start()
	{
		var containerBuilder = new ContainerBuilder();
		var registry = new AutofacComponentRegistry(containerBuilder);

		ServiceBus.Register(registry);

		_bus = ServiceBus.Create(new AutofacComponentResolver(containerBuilder.Build())).Start();
	}

	public void Dispose()
	{
		_bus.Dispose();
	}
}

A bit of configuration is going to be needed to help things along:

<configuration>
	<configSections>
		<section name="serviceBus" type="Shuttle.Esb.ServiceBusSection, Shuttle.Esb"/>
	</configSections>

	<serviceBus>
		<inbox 
			workQueueUri="msmq://./shuttle-server-work" 
			deferredQueueUri="msmq://./shuttle-server-deferred" 
			errorQueueUri="msmq://./shuttle-error" />
	</serviceBus>
</configuration>

Set Shuttle.Core.Host.exe as the Start external program option by navigating to the bin\debug folder of the server project for the Shuttle.Deferred.Server project.

It may be necessary to build the solution before the Shuttle.Core.Host.exe executable will be available in the bin\debug folder.

Send a command message for processing

var container = new WindsorComponentContainer(new WindsorContainer());

ServiceBus.Register(container);

using (var bus = ServiceBus.Create(container).Start())
{
	bus.Send(new RegisterMemberCommand
	{
		UserName = "Mr Resistor",
		EMailAddress = "ohm@resistor.domain"
	});
}

Publish an event message when something interesting happens

var smRegistry = new Registry();
var registry = new StructureMapComponentRegistry(smRegistry);

ServiceBus.Register(registry); // will using bootstrapping to register SubscriptionManager

using (var bus = ServiceBus
	.Create(
		new StructureMapComponentResolver(
		new Container(smRegistry)))
	.Start())
{
	bus.Publish(new MemberRegisteredEvent
	{
		UserName = "Mr Resistor"
	});
}

Subscribe to those interesting events

SubscriptionManager.Default().Subscribe<MemberRegisteredEvent>();

Handle any messages

public class RegisterMemberHandler : IMessageHandler<RegisterMemberCommand>
{
	public void ProcessMessage(IHandlerContext<RegisterMemberCommand> context)
	{
		Console.WriteLine();
		Console.WriteLine("[MEMBER REGISTERED] : user name = '{0}'", context.Message.UserName);
		Console.WriteLine();

		context.Publish(new MemberRegisteredEvent
		{
			UserName = context.Message.UserName
		});
	}
}
public class MemberRegisteredHandler : IMessageHandler<MemberRegisteredEvent>
{
	public void ProcessMessage(IHandlerContext<MemberRegisteredEvent> context)
	{
		Console.WriteLine();
		Console.WriteLine("[EVENT RECEIVED] : user name = '{0}'", context.Message.UserName);
		Console.WriteLine();
	}
}