Conventions

A convention is a class that Mocha calls for each configuration of one kind while it builds the bus. It receives the configuration of a message type or an endpoint and can change it before Mocha uses it. Mocha assigns the default urn:message: identity of message types with a built-in convention.

Use a convention when a setting applies to many message types or endpoints. To configure a single message type or endpoint, use AddMessage<T>() or the endpoint descriptor instead.

Write a convention#

A convention implements the interface for the configuration it changes. This convention limits every receive endpoint that handles messages to four concurrent messages, unless the endpoint sets its own limit:

C#
public sealed class EndpointConcurrencyConvention : IReceiveEndpointConvention
{
    public void Configure(IMessagingConfigurationContext context, ReceiveEndpointConfiguration configuration)
    {
        if (configuration is { Kind: ReceiveEndpointKind.Default, MaxConcurrency: null })
        {
            configuration.MaxConcurrency = 4;
        }
    }
}

Register a convention#

Register a convention on the bus to apply it on every transport:

C#
builder.Services
    .AddMessageBus()
    .AddConvention(new EndpointConcurrencyConvention())
    .AddRabbitMQ();

Register it on a transport to apply it only to the endpoints of that transport:

C#
builder.Services
    .AddMessageBus()
    .AddRabbitMQ(transport => transport.AddConvention(new EndpointConcurrencyConvention()));

Message types use only the conventions registered on the bus.

Convention interfaces#

InterfaceCalled forSettings you can change
IMessageTypeConfigurationConventionEvery message typeIdentity, DefaultContentType, MessageSerializer
IReceiveEndpointConventionEvery receive endpointMaxConcurrency, AutoProvision, BindMode, ReceiveMiddlewares
IConfigurationConvention<DispatchEndpointConfiguration>Every dispatch endpointDispatchMiddlewares
IRabbitMQReceiveEndpointConfigurationConvention, IAzureServiceBusReceiveEndpointConfigurationConvention, IPostgresReceiveEndpointConfigurationConvention, IInMemoryReceiveEndpointConfigurationConventionEvery receive endpoint of that transportThe endpoint settings of that transport

Order#

Mocha first applies the settings of the single message type or endpoint, such as AddMessage<T>() or an endpoint's MaxConcurrency(...). Then it calls the conventions registered on the bus, in the order you registered them. After those, it assigns the default urn:message: identity to a message type that still has none, and calls the conventions registered on the transport of an endpoint.

A convention that sets a value replaces the setting of the single message type or endpoint. Check that a value is unset before you set it.

Example: message identities#

For a convention that sets the identity of every message type, see How message type resolution works.

Edit this page on GitHub
Maintained by ChilliCream. Last updated on by PascalSenn