A message is the unit of communication for NServiceBus. There are two types of messages, commands and events, that capture more of the intent and help NServiceBus enforce messaging best practices. This enforcement is enabled by default but can be disabled.
Command | Event |
---|---|
Used to make a request to perform an action. | Used to communicate that an action has been performed. |
Has one logical owner. | Has one logical owner. |
Should be sent to the logical owner. | Should be published by the logical owner. |
Cannot be published. | Cannot be sent. |
Cannot be subscribed to or unsubscribed from. | Can be subscribed to and unsubscribed from. |
Can be sent using the gateway. | Cannot be sent using the gateway. |
In a request and response pattern, reply messages are neither a command nor an event.
Validation
There are checks in place to ensure best practices are followed. Violations of the above guidelines generate the following exceptions:
- "Pub/Sub is not supported for Commands. They should be sent directly to their logical owner." - this exception is being thrown when attempting to publish a Command or subscribe to/unsubscribe from a Command.
- "Events can have multiple recipients so they should be published." - this exception will occur when attempting to use
Send()
to send an event. - _"Reply is neither supported for Commands nor Events. Commands should be sent to their logical owner using bus.Send and bus. Events should be published." - this exception is thrown when attempting to reply with a Command or an Event.
- "Cannot configure routing for type because it is not considered a message. Message types have to either implement NServiceBus.IMessage interface or match a defined message convention." - this exception is thrown when configuring the destination endpoint for a non-message type.
- "Cannot configure routing for assembly because it contains no types considered as messages. Message types have to either implement NServiceBus.IMessage interface or match a defined message convention." - this exception is thrown when configuring the destination endpoint for an assembly that contains no types considered messages.
- "Cannot configure routing for namespace because it contains no types considered as messages..." - this exception is thrown when configuring the destination endpoint for a namespace that contains no types considered messages.
- "Cannot configure publisher for type because it is not considered a message. Message types have to either implement NServiceBus.IMessage interface or match a defined message convention." - this exception is thrown when configuring the publisher for a type that is not a message.
- "Cannot configure publisher for type because it is not considered an event. Event types have to either implement NServiceBus.IEvent interface or match a defined event convention." - this exception is thrown when configuring the publisher for a type that is not an event.
- "Cannot configure publisher for type because it is a command." - this exception is thrown when configuring the publisher for a command.
Designing messages
Messages should:
- Be simple POCO types
- Be as small as possible
- Satisfy the Single Responsibility Principle
- Favor simplicity and redundancy over object-oriented practices like inheritance
- Not be re-used for other purposes (e.g., domain objects, data access objects, or UI binding objects)
Prior to NServiceBus version 7.2, messages had to be defined as a class
. Defining them as a struct
would result in a runtime exception.
Generic message definitions (e.g., MyMessage
) are not supported. It is recommended to use dedicated, simple types for each message.
Messages define the data contracts between endpoints. More details are available in the sharing message contracts documentation.
By following these guidelines, message types are generally more compatible with serializers and tend to be more evolvable over time.
Identifying messages
Endpoints will process any message that can be deserialized into a .NET type but requires message contracts to be identified upfront to support:
- Automatic subscriptions for event types
- Routing based on
namespace
orassembly
for commands
Messages can be defined by implementing a marker interface or specifying a custom convention.
Marker interfaces
The simplest way to identify messages is to use interfaces.
NServiceBus.
for a command.ICommand NServiceBus.
for an event.IEvent NServiceBus.
for any other message type (e.g., a reply in a request/response pattern).IMessage
public class MyCommand : ICommand { }
public class MyEvent : IEvent { }
public class MyMessage : IMessage { }
Conventions
To avoid having message contract assemblies reference the NServiceBus assembly, custom conventions can be used to identify the types used as contracts for messages, commands, and events. This is known as unobtrusive mode.