Publish and Handle an Event

Component: NServiceBus | Nuget: NServiceBus (Version: 5.x)

Defining events

The messages need to be declared as an event before they can be published. That can be done with marker interfaces or using message conventions.

Via a Marker interface

Add IEvent marker interface to the message definition:

Edit
namespace Domain.Messages
{
    public class UserCreatedEvent :
        IEvent
    {
        public string Name { get; set; }
    }
}

Via Message Conventions

The following message:

Edit
namespace Domain.Messages
{
    public class UserCreatedEvent
    {
        public string Name { get; set; }
    }
}

can be declared as an event using the following convention:

Edit
var conventions = busConfiguration.Conventions();
conventions.DefiningEventsAs(
    type =>
    {
        return type.Namespace != null &&
               type.Namespace.StartsWith("Domain") &&
               type.Name.EndsWith("Event");
    });

Learn more about conventions and unobtrusive mode.

Handling an event

In order to handle an event, implement IHandleMessages<T> interface in any Handler or Saga class, where T is the specific event type.

Publishing an event

In order to publish an event call the Publish method.

There are a few common scenarios for publishing events. Events might be published:

  • from a handler, when processing another message.
Edit
public class CreateUserHandler :
    IHandleMessages<CreateUserCommand>
{
    IBus bus;

    public CreateUserHandler(IBus bus)
    {
        this.bus = bus;
    }

    public void Handle(CreateUserCommand message)
    {
        var userCreatedEvent = new UserCreatedEvent
        {
            Name = message.Name
        };
        bus.Publish(userCreatedEvent);
    }
}
  • from a saga handler, when processing another message.
Edit
public class CreateUserSaga :
    Saga<CreateUserSaga.SagaData>,
    IHandleMessages<CreateUserCommand>
{
    IBus bus;

    public CreateUserSaga(IBus bus)
    {
        this.bus = bus;
    }

    public void Handle(CreateUserCommand message)
    {
        var userCreatedEvent = new UserCreatedEvent
        {
            Name = message.Name
        };
        bus.Publish(userCreatedEvent);
    }
  • at endpoint startup
Edit
using (var bus = Bus.Create(busConfiguration).Start())
{
    bus.Publish(new MyEvent());

Events as Classes or Interfaces

NServiceBus messages can be implemented either as classes or interfaces. Since interfaces cannot be instantiated directly, use the following API to publish events implemented as interfaces:

Edit
bus.Publish<IMyEvent>(
    messageConstructor: message =>
    {
        message.SomeProperty = "Hello world";
    });

When the event message is declared as an interface, NServiceBus will generate a proxy, set properties and publish the message. It's equivalent to the following call:

Edit
var message = new MyEvent
{
    SomeProperty = "Hello world"
};
bus.Publish(message);

Samples

Related Articles


Last modified