When using NServiceBus you define your message contracts using plain C# classes or interfaces. In order for NServiceBus to find those classes when scanning your assemblies you need to mark them with the special IMessage interface.
This essentially tells us that “hey this is a message definition, please use it”. This might seem like a small thing but now you’re coupling your message contracts to a NServiceBus assembly since you need to reference the NServiceBus.dll in order get access to the interface.
This dependency can cause problems if you have different services that run different versions of NServiceBus. Jonathan Oliver has a great write up on this very subject.
This is not a big deal for commands because they are always used with in the boundary of a single service and it’s fair to require a service to use the same version of NServiceBus. But when it comes to events this becomes more of a problem since requiring all of your services to use the same version of NServiceBus and there by forcing them to upgrade NServiceBus all at once is not an ideal thing.
There are a couple of ways you can solve this. NServiceBus V3 has a few changes that will help you further.
- NServiceBus follows the semver.org semantics. We only change our assembly version when we make changes that are not backwards compatible or we introduce substantial new functionality or improvements. This mean that 3.0.1 and 3.0.X will have the same assembly version (3.0.0), file version is of course changed for every release/build. This means that as long as you do a nuget update with the -safe flag your service contracts will stay compatible.
- Support for running in what we call “Unobtrusive” mode. Running in unobtrusive mode removes the need for you to reference any NServiceBus assemblies from your own message assemblies. This obviously removes the problem altogether.
This new feature in NServiceBus V3 allows you to pass in your own conventions to determine which types are message definitions instead of using the IMessage, ICommand or IEvent interfaces.
Below is a snippet that shows how to define those conventions:
Configure.With() .DefaultBuilder() .FileShareDataBus(@"\\MyDataBusShare\") .DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Commands")) .DefiningEventsAs(t => t.Namespace != null && t.Namespace.EndsWith("Events")) .DefiningMessagesAs(t => t.Namespace == "Messages") .DefiningEncryptedPropertiesAs(p => p.Name.StartsWith("Encrypted")) .DefiningDataBusPropertiesAs(p => p.Name.EndsWith("DataBus")) .DefiningExpressMessagesAs(t => t.Name.EndsWith("Express")) .DefiningTimeToBeReceivedAs(t => t.Name.EndsWith("Expires") ? TimeSpan.FromSeconds(30) : TimeSpan.MaxValue);
The code above tells NServiceBus to treat all types with a namespace that ends with “Messages” as messages. As you see you can also specify conventions for the new ICommand and IEvent feature.
NServiceBus supports property level encryption by using a special WireEncryptedString property. The example above shows the unobtrusive way to tell NServiceBus which properties you want to be encrypted. These properties need to be of type String.
The example above also shows the unobtrusive way to tell NServiceBus which properties will be delivered on a separate channel from the message itself using the Data Bus feature, and which messages are express or/and have a time to be received.
Last note before closure: the .DefiningXXXAs() has to be before the .UnicastBus() when you're self hosting, otherwise you will get "System.InvalidOperationException: "No destination specified for message(s): <message type name>".