Unobtrusive


NuGet packages

6-pre NServiceBus
5.x NServiceBus
This topic contains pre-release documentation (for Version 6-pre) and is subject to change prior to the final release.

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:

6-pre NServiceBus
public static void ApplyCustomConventions(this EndpointConfiguration endpointConfiguration)
{
    ConventionsBuilder conventions = endpointConfiguration.Conventions();
    conventions.DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Commands"));
    conventions.DefiningEventsAs(t => t.Namespace != null && t.Namespace.EndsWith("Events"));
    conventions.DefiningMessagesAs(t => t.Namespace == "Messages");
    conventions.DefiningEncryptedPropertiesAs(p => p.Name.StartsWith("Encrypted"));
    conventions.DefiningDataBusPropertiesAs(p => p.Name.EndsWith("DataBus"));
    conventions.DefiningExpressMessagesAs(t => t.Name.EndsWith("Express"));
    conventions
        .DefiningTimeToBeReceivedAs(t => t.Name.EndsWith("Expires")
            ? TimeSpan.FromSeconds(30)
            : TimeSpan.MaxValue
        );
}
5.x NServiceBus
public static void ApplyCustomConventions(this BusConfiguration busConfiguration)
{
    ConventionsBuilder conventions = busConfiguration.Conventions();
    conventions.DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Commands"));
    conventions.DefiningEventsAs(t => t.Namespace != null && t.Namespace.EndsWith("Events"));
    conventions.DefiningMessagesAs(t => t.Namespace == "Messages");
    conventions.DefiningEncryptedPropertiesAs(p => p.Name.StartsWith("Encrypted"));
    conventions.DefiningDataBusPropertiesAs(p => p.Name.EndsWith("DataBus"));
    conventions.DefiningExpressMessagesAs(t => t.Name.EndsWith("Express"));
    conventions
        .DefiningTimeToBeReceivedAs(t => t.Name.EndsWith("Expires")
            ? TimeSpan.FromSeconds(30)
            : TimeSpan.MaxValue
        );
}
4.x NServiceBus
public static void ApplyCustomConventions(this Configure configure)
{
    configure.FileShareDataBus(@"..\..\..\DataBusShare\");
    configure.DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Commands"));
    configure.DefiningEventsAs(t => t.Namespace != null && t.Namespace.EndsWith("Events"));
    configure.DefiningMessagesAs(t => t.Namespace == "Messages");
    configure.DefiningEncryptedPropertiesAs(p => p.Name.StartsWith("Encrypted"));
    configure.DefiningDataBusPropertiesAs(p => p.Name.EndsWith("DataBus"));
    configure.DefiningExpressMessagesAs(t => t.Name.EndsWith("Express"));
    configure.DefiningTimeToBeReceivedAs(t => t.Name.EndsWith("Expires") ? TimeSpan.FromSeconds(30) : 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.

The above code instructs NServiceBus to encrypt any property that starts with the string Encrypted and resides in any class in the namespaces that ends with Command or Events, or in namespaces that are equal to Messages.

The encryption algorithm is declared in app.config of both client and server with the RijndaelEncryptionServiceConfig section name. See the Encryption. NServiceBus supports property level encryption by using a special WireEncryptedString property. The code snippet shows the unobtrusive way to tell NServiceBus which properties to encrypt.

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 are express and/or 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.
  • Message: the message definitions with no reference to NServiceBus.

Related Articles


Last modified 2016-04-18 00:37:52Z