views:

41

answers:

1

Is there any way to intercept messages in NServiceBus?

From now i can do it manually via introducing base message handler like this:

public abstract class MessageHandler<T> : IHandleMessages<T>
    where T : IMessage
{
    public IBus Bus { get; set; }

    protected abstract void HandleCommand(T command);

    public void Handle(T command)
    {
        // perform some logic on *command* before
        HandleCommand(command);
        // perform some logic on *command* after
    }
}

And the usage:

public class ConcreteMessageHandler : MessageHandler<ConcreteMessage>
{
    protected override void HandleCommand(ConcreteMessage message)
    {
        //handle command
    }
}

But doing this way i'm loosing an ability to subscribe to multiple messages (because i cannot inherit from multiple MessageHandler<> classes).

A: 

Take a look at the message module feature http://jonathan-oliver.blogspot.com/2010/04/nservicebus-message-modules.html

Andreas
Sorry for confusing you with code sample. I've updated comments in Handle() method. As title said i'm looking for message interception. And as you know, NServiceBus Modules cannot be used for this, because they wrap transport messages, rather then logical.
Rationalle
Ah, then your options are to either create additional messagehandlers for the same message that you configure to run before and after your regular handler. Another option would be to use the interception capabilitites of your container. (http://structuremap.net/structuremap/Interception.htm if you're on structuremap). Can you elaborate on what you're trying to do before and after your command?
Andreas
Thank you Andreas for this two points. One of the things i trying to do is to send received message to some another queue only in case it was correctly handled. ForwardReceivedMessagesTo send all messages.Seems that for my needs i should use either container interception or my dumb base message handler.
Rationalle
Also i want to intercept a moment of exception in message handler to inform another queue about this (event though NServiceBus says do not handle errors - i think in many cases we still need to handle errors and we need to know exactly on what message exception was thrown...)
Rationalle