Question

Does NServiceBus guarantee that a single message isn't handled by more than one thread?

I've been working under this assumption, however it seems that a single message was handled twice at the same time.

We do some non-transactional things in our handler so in our config we specify DoNotWrapHandlersExecutionInATransactionScope. Does this affect NServiceBus's ability to ensure that a message is only handled once? As I understand it, this just does exactly what it says and doesn't wrap with a transaction scope?

Can someone clear this up for me? What else could cause a duplicate message issue?

Was it helpful?

Solution

Let's say your handler looks something like this:

public class SomeCommandHandler : IHandleMessages<SomeCommnand>
{
    public void Handle(SomeCommnand message)
    {
        // write to the database and save changes

        throw new Exception("Oh noes!");
    }
}

If you don't have your handler wrapped in a TransactionScope, I'm guessing (so please test on your own), that your data would be written but your message would be retried (rightfully so, because it failed). This would give you the impression that you got the message twice.

But why are you doing anything that's non-transactional? That kinda defeats the purpose of NServiceBus.

If you are doing things like HTTP requests or other long-running things along with your basic DB interaction, then you want to use a Saga, not a simple command handler.

OTHER TIPS

Regarding

We do some non-transactional things in our handler so in our config we specify DoNotWrapHandlersExecutionInATransactionScope.

You can still do non-transactional things inside a handler without setting DoNotWrapHandlersExecutionInATransactionScope.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top