• Enterprise Service Bus libraries for .NET – Shuttle ESB

    One in a series of posts giving a quick overview of ESB libraries for .NET

    Shuttle ESB describes itself as a “free .NET open-source enterprise service bus”. It supports a good range of transports and integrates with a number of IoC containers.

    https://github.com/Shuttle/shuttle-esb

    Messages

    A POCO class

    Publishing

    Publish using IServiceBus. You’ll also make use of static methods from the ServiceBus class.

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

    Subscribing

    A handler implements IMessageHandler

    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();
        }
    }
    

    You also need to tell the subscription manager that you want to subscribe to a particular event. resolver is a wrapper class for your specific container.

    resolver.Resolve<ISubscriptionManager>().Subscribe<MemberRegisteredEvent>();
    

  • Enterprise Service Bus libraries for .NET – Nimbus

    One in a series of posts giving a quick overview of ESB libraries for .NET

    Nimbus primarily supports the Azure Service Bus as a transport, so if your application is going to run in Azure, then this could be a good choice.

    https://github.com/NimbusAPI/Nimbus/wiki/Publishing-an-Event-on-the-Bus

    Messages

    Event messages should implement IBusEvent

    Publishing

    Publish with an instance of IBus

    _bus.Publish(new NewOrderReceived {CustomerName = "Ricky Bobby"});
    

    Subscribing

    Handle events by implementing IHandleMulticastEvent

    public class ListenForNewOrders : IHandleMulticastEvent
    {
    
        public async Task Handle(NewOrderReceived busEvent)
        {
            Console.WriteLine("I heard about a new order from " + busEvent.CustomerName);
    
            //Do more stuff
        }
    }
    

  • Enterprise Service Bus libraries for .NET – MassTransit

    One in a series of posts giving a quick overview of ESB libraries for .NET

    MassTransit is an open-source library which apparently began as a response to some perceived deficiencies in the version of NServiceBus that was around in 2007. The project did a major rewrite in 2014, introducing async support and switching from MSMQ to RabbitMQ as the preferred transport.

    Messages

    Messages are POCO classes. There is no requirement to inherit from any base class or implement a specific interface.

    Publishing

    http://masstransit-project.com/MassTransit/usage/producing-messages.html

    Messages are published using IPublishEndpoint, which is provided by ConsumeContext for consumers or by the IBus interface

    public async Task NotifyOrderSubmitted(IPublishEndpoint publishEndpoint)
    {
        await publishEndpoint.Publish<OrderSubmitted>(new
        {
            OrderId = "27",
            OrderDate = DateTime.UtcNow,
        });
    }
    

    Subscribing

    http://masstransit-project.com/MassTransit/usage/message-consumers.html

    A subscribing class (or consumer) implements the IConsumer<T> interface.

    public class UpdateCustomerConsumer :
        IConsumer<UpdateCustomerAddress>
    {
        public async Task Consume(ConsumeContext<UpdateCustomerAddress> context)
        {
            await Console.Out.WriteLineAsync($"Updating customer: {context.Message.CustomerId}");
    
            // update the customer address
        }
    }
    

    A consumer needs to be connected to an endpoint to receive messages

    var busControl = Bus.Factory.CreateUsingRabbitMq(cfg =>
    {
        var host = cfg.Host(new Uri("rabbitmq://localhost/"), h =>
        {
            h.Username("guest");
            h.Password("guest");
        });
    
        cfg.ReceiveEndpoint(host, "customer_update_queue", e =>
        {
            e.Consumer<UpdateCustomerConsumer>();
        });
    });