Publish and Handle an Event

Component: NServiceBus
NuGet Package NServiceBus (5.x)

Defining events

Messages must be declared as an event before they can be published. This can be done with interfaces or using message conventions.

Via an interface

Add the IEvent interface to the message definition:

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

Via message conventions

Consider the following message:

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

This can be declared as an event using the following convention:

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 the IHandleMessages<T> interface in any handler or saga class, where T is the specific event type.

Publishing an event

Call the Publish method to publish an event.

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

  • from a handler, when processing another message.
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.
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
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 can not be instantiated directly, use the following API to publish events implemented as interfaces:

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:

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

Samples

  • Publish/Subscribe
    Publish/Subscribe, fault-tolerant messaging, and durable subscriptions.

Related Articles


Last modified