I am currently developing a sample project where I use NServiceBus in the back-end to publish events. The back-end is then supposed to be handling its own events through a set of handlers. This works fine when I implement one specific handler for each event like so:
public sealed class MyEventHandler : IHandleMessages<MyEvent> { }
public sealed class OtherEventHandler : IHandleMessages<OtherEvent> { }
However, since these handlers just dispatch the events to another processor, I wanted to clean up a bit and implement only a single generic message handler:
public sealed class GenericHandler : IHandleMessages<object> { }
Unfortunately, the generic handler is only invoked for message types for which another, specific handler is present in the project. In other words, the GenericHandler correctly receives the MyEvent event when I leave the MyEventHandler in the sources, but stops receiving this message when I delete this (now obsolete) handler. My goal is in fact to remove all specific handlers from the project and to work only with a single GenericHandler. Am I missing a basic step in configuration?
The NServiceBus-config looks like this:
<UnicastBusConfig>
<MessageEndpointMappings>
<add Assembly="Kingo.Samples.Chess.Api" Endpoint="kingo.samples.chess" />
</MessageEndpointMappings>
</UnicastBusConfig>
Furthermore:
- The Api-assembly mentioned above contains all events that need to be published and handled.
- I marked all messages with the
ICommand
andIEvent
marker interfaces from NServiceBus. - I use NServiceBus V5 (Core) in combination with NServiceBus.Host V6.
I have the following custom bus configuration in my EndpointConfig:
void IConfigureThisEndpoint.Customize(BusConfiguration configuration) { configuration.AssembliesToScan(GetAssembliesToScan("*Chess.Api.dll", "*Chess.dll")); configuration.UsePersistence<InMemoryPersistence>(); configuration.UseContainer<UnityBuilder>(); configuration.UseSerialization<JsonSerializer>(); configuration.Conventions().DefiningEventsAs(type => type.Name.EndsWith("Event")); }