0
votes

In the unobtrusive NServiceBus sample it explains how to map your inbound messages to ICommand / IEvent / IMessage in the endpoint configuration as follows:

.DefiningCommandsAs(t => t.Namespace != null && t.Namespace.EndsWith("Commands"))
.DefiningEventsAs(t => t.Namespace != null && t.Namespace.EndsWith("Events"))
.DefiningMessagesAs(t => t.Namespace == "Messages")

But what about the following example, where I'd like to create a marker interface and have all my events implement it:

public interface IAmSomeEvent
{
}

public class SomethingImportantHappenned : IAmSomeEvent
{
    public string blah { get; set; }
}

and then do something like this:

.DefiningEventsAs(t => t.GetInterfaces().Contains(typeof(IAmSomeEvent)))

But the problem with this is it doesn't work (NSB does not map this to IEvent).

I can kind of see why this doesn't work as NSB is just receiving a stream of json (or XML) and so it doesn't really care that the original type happens to implement some interface or other. But it would be a very nice feature.

Does anyone have any suggestions about how to achieve this?

Many thanks

1
make sure that your messages are included in the list of scanned typesAndreas Öhlund
Hi Andreas. Thanks for this. So if the assembly containing my messages is in the same directory as nservicebus.host.exe should it not be scanned?tom redfern
All assemblies are scanned by default. Does the convention work if you hardcode t== typeof(SomethingImportantHappenned) ? Just to rule out issues with asm scanningAndreas Öhlund
Hi Andreas thanks for your response but I have been sidetracked onto another issue - please would you be kind enough to take a look? stackoverflow.com/questions/14124428tom redfern

1 Answers

4
votes

You don't need your interface to map to that of NServiceBus, nor is the problem anything to do with how it does deserialization.

I think that your events inherit from something else which itself inherits from your marker interface, and that that's why your code doesn't work. You see, GetInterfaces only returns the interfaces directly implemented by your class, and not the full inheritance hierarchy.

Instead, check to see if t.IsAssignableFrom(typeof(IAmSomeEvent)).

All that being said, I'm not sure I see the point of this - after all, the conventions were introduced to provide decoupling from infrastructure so that you could have publishers and subscribers depend on different versions of NServiceBus. By introducing your own interface, you seem to be reintroducing that kind of dependency - in short, why not just use the NServiceBus IEvent interface?