Tuesday, 14 November 2017

Enterprise Service Bus libraries for .NET – NServiceBus

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

Originally an open-source library developed by Udi Dahan, the licensing was changed after version 2 to put it on a more commercial footing, and the software is now developed and supported by Udi’s company – Particular Software. The source code remains on Github.

NServiceBus is a tried and tested library. It has grown a lot since it's humble beginnings and now is once part of a suite of tools and libraries for building distributed applications. It's no surprise that Udi Dahan just happens to be highly regarded as an expert in distributed software architecture.



Messages need to either implement a marker interface or use defined conventions.
namespace Domain.Messages
  public class UserCreatedEvent : IEvent
    public string Name { get; set; }
var conventions = endpointConfiguration.Conventions();

 type =>

       return type.Namespace == "MyNamespace.Messages.Events";


You can publish an event from within another handler or when you create an endpoint.
This example shows publishing an event from an endpoint:

var endpointInstance = await Endpoint.Start(endpointConfiguration)
    await endpointInstance.Publish(new MyEvent())


To handle an event, implement IHandleMessages<T>

public class UserCreatedHandler : IHandleMessages<UserCreatedEvent>
  public Task Handle(UserCreatedEvent message, IMessageHandlerContext context)
  { … }

NServiceBus will automatically locate handlers by looking for implementations of IHandleMessages<T>

No comments: