Unobtrusive

Component: NServiceBus
NuGet Package NServiceBus (5.x)

Run the solution. Two console applications should start up, Client and Server.

Configuring the Unobtrusive message

Look at the ConventionExtensions in the SharedConventions project. The code tells NServiceBus how to determine which types are message definitions by passing in custom conventions, instead of using the IMessage, ICommand, or IEvent interfaces:

public static void ApplyCustomConventions(this BusConfiguration busConfiguration)
{
    var conventions = busConfiguration.Conventions();
    conventions.DefiningCommandsAs(
        type =>
        {
            return type.Namespace != null &&
                   type.Namespace.EndsWith("Commands");
        });
    conventions.DefiningEventsAs(
        type =>
        {
            return type.Namespace != null &&
                   type.Namespace.EndsWith("Events");
        });
    conventions.DefiningMessagesAs(
        type =>
        {
            return type.Namespace == "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;
        });
}

The code tells NServiceBus to treat all types with a namespace that ends with "Messages" the same as for messages that explicitly implement IMessage.

It also shows the unobtrusive way to tell NServiceBus which properties to deliver on a separate channel from the message itself using the Data Bus feature, and which messages have a defined time to be received.

Look at the code. There are a number of projects in the solution:

  • Client: sends a request and a command to the server and handles a published event
  • Server: handles requests and commands, and publishes events
  • Shared: Shared code including the message conventions and message definitions.

Related Articles


Last modified