Publish and Handle an Event

Component: NServiceBus
NuGet Package NServiceBus (7.2-pre)
This page targets a pre-release version and is subject to change prior to the final release.

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:

7.x - 7.1 NServiceBus
namespace Domain.Messages
{
    public class UserCreatedEvent :
        IEvent
    {
        public string Name { get; set; }
    }
}

Via message conventions

Consider the following message:

7.x - 7.1 NServiceBus
namespace Domain.Messages
{
    public class UserCreatedEvent
    {
        public string Name { get; set; }
    }
}

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

7.x - 7.1 NServiceBus
var conventions = endpointConfiguration.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.
7.x - 7.1 NServiceBus
public class CreateUserHandler :
    IHandleMessages<CreateUserCommand>
{
    public Task Handle(CreateUserCommand message, IMessageHandlerContext context)
    {
        var userCreatedEvent = new UserCreatedEvent
        {
            Name = message.Name
        };
        return context.Publish(userCreatedEvent);
    }
}
  • from a saga handler, when processing another message.
7.x - 7.1 NServiceBus
public class CreateUserSaga :
    Saga<CreateUserSaga.SagaData>,
    IHandleMessages<CreateUserCommand>
{
    public Task Handle(CreateUserCommand message, IMessageHandlerContext context)
    {
        var userCreatedEvent = new UserCreatedEvent
        {
            Name = message.Name
        };
        return context.Publish(userCreatedEvent);
    }
  • at endpoint startup
7.x - 7.1 NServiceBus
// Other config
var endpointInstance = await Endpoint.Start(endpointConfiguration)
    .ConfigureAwait(false);
await endpointInstance.Publish(new MyEvent())
    .ConfigureAwait(false);

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:

7.x - 7.1 NServiceBus
return context.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:

7.x - 7.1 NServiceBus
var message = new MyEvent
{
    SomeProperty = "Hello world"
};
await context.Publish(message)
    .ConfigureAwait(false);

Samples

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

Related Articles


Last modified