Conventions

Component: NServiceBus
NuGet Package NServiceBus (7.x - 7.1)

A convention is a way of defining what a certain type is instead of using an interface or an attribute. Using conventions along with avoiding references to NServiceBus assemblies is referred to as unobtrusive mode. This is ideal for use in cross-platform environments.

Currently conventions exist to identify:

Messages can be defined in a Portable Class Library (PCL) and shared across multiple platforms even if the platform does not use NServiceBus for message processing.

var conventions = endpointConfiguration.Conventions();
conventions.DefiningCommandsAs(
    type =>
    {
        return type.Namespace == "MyNamespace.Messages.Commands";
    });
conventions.DefiningEventsAs(
    type =>
    {
        return type.Namespace == "MyNamespace.Messages.Events";
    });
conventions.DefiningMessagesAs(
    type =>
    {
        return type.Namespace == "MyNamespace.Messages";
    });
conventions.DefiningDataBusPropertiesAs(
    property =>
    {
        return property.Name.EndsWith("DataBus");
    });
conventions.DefiningExpressMessagesAs(
    type =>
    {
        return type.Name.EndsWith("Express");
    });
conventions.DefiningTimeToBeReceivedAs(
    type =>
    {
        if (type.Name.EndsWith("Expires"))
        {
            return TimeSpan.FromSeconds(30);
        }
        return TimeSpan.MaxValue;
    });
Note that in .NET, the namespace is optional and hence can be null. If any conventions do partial string checks, for example using EndsWith or StartsWith, then a null check should be used. So include .Namespace != null at the start of the convention. Otherwise a null reference exception will occur during the type scanning.

Related Articles


Last modified