Controlling what is subscribed

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

Automatic subscriptions

The default mode for managing subscriptions is auto subscribe. Every time a subscriber endpoint is started, it determines which events it needs to subscribe to and automatically subscribes to them. For more information on how publish and subscribe works, refer to Publish-Suscribe.

Messages matching the following criteria will be auto subscribed at startup.

  1. Defined as an event either using IEvent or by the .DefiningEventsAs convention.
  2. At least one message handler and/or saga exists for the given event.
  3. If the selected transport does not support publish-subscribe natively, the publisher for that message needs to be specified via the routing API.

Exclude sagas from auto subscribe

Sagas are treated the same as handlers and will cause an endpoint to subscribe to a given event. It is possible to opt-in to the old exclude saga event handling behavior using:

var autoSubscribe = endpointConfiguration.AutoSubscribe();

Auto subscribe to plain messages

This is a bad practice. Subscriptions should always be based on events.

In Versions 6 and above it is possible to subscribe to messages not defined as events by manually subscribing to the message type.

When a subscriber stops or uninstalls

A Subscriber will not unsubscribe when it stops, it will remain registered at the publisher to receive events. The publisher still sends events to the queue of the stopped subscriber. When the subscriber is started it will consume the messages from its queue. The subscriber will never lose an event.

Disabling auto-subscription

Automatic subscriptions by the infrastructure can be disabled using the configuration API:


Manually subscribing to a message

Events can manually be subscribed and unsubscribed:

await endpoint.Subscribe<MyEvent>()

await endpoint.Unsubscribe<MyEvent>()

In Versions 6 and above, Subscribe and Unsubscribe are accessible via the IMessageSession available on the IEndpointInstance or within a Feature startup task.

Last modified