Øyvind
Øyvind

Reputation: 1610

NServiceBus: Messages handled multiple times

I am at a complete loss as to why I am experiencing this problem. I am new to NServiceBus and have so far set up a dead simple 'server' which listens for messages sent by a web application. The server asks for custom initialisation (IWantCustomInitialization) and uses a custom builder for Castle Windsor 2.5.1. This custom builder is basically a copy of the one that comes with the NServiceBus source code, with two minor changes to move away from methods deprecated in Windsor 2.5.

Note that my code shares the container instance with NServiceBus.

The problem I experience is that every message sent by the web application is processed five (5) times by the server. The log files have five entries for each attempt, with the fifth attempt looking like this:

2011-03-28 16:04:10,326 [Worker.8] DEBUG NServiceBus.Unicast.UnicastBus [] - Calling 'HandleEndMessage' on NServiceBus.SagaPersisters.NHibernate.NHibernateMessageModule
2011-03-28 16:04:10,327 [Worker.8] DEBUG NServiceBus.Unicast.UnicastBus [] - Calling 'HandleEndMessage' on Server.NHibernateSessionMessageModule
2011-03-28 16:04:10,341 [Worker.8] DEBUG NServiceBus.Unicast.UnicastBus [] - Calling 'HandleError' on NServiceBus.SagaPersisters.NHibernate.NHibernateMessageModule
2011-03-28 16:04:10,342 [Worker.8] DEBUG NServiceBus.Unicast.UnicastBus [] - Calling 'HandleError' on Server.NHibernateSessionMessageModule
2011-03-28 16:04:10,344 [Worker.8] ERROR NServiceBus.Unicast.Transport.Msmq.MsmqTransport [] - Message has failed the maximum number of times allowed, ID=80cffd98-a5bd-43e0-a482-a2d96ca42b22\20677.

I have no indication why the message fails, and I don't know where to dig for more information/output.

The configuration 'endpoint' looks like this:

public void Init()
{
    container = Windsor.Container;
    NServiceBus.Configure.With().CastleWindsor251Builder(container).XmlSerializer().MsmqTransport().IsolationLevel(System.Transactions.IsolationLevel.Unspecified);

    var masterInstaller = new NotificationServerInstaller();
    masterInstaller.Install(container, null);
}

The message handler is, at this stage, really contrived, and looks like this:

public class NewUserMessageHandler : IHandleMessages<NotifyNewUserMessage>
{
    private readonly IGetUserQuery _getUserQuery;

    public NewUserMessageHandler(IGetUserQuery getUserQuery)
    {
        _getUserQuery = getUserQuery;
    }

    public void Handle(NotifyNewUserMessage message)
    {
        var result = _getUserQuery.Invoke(new GetUserRequest { Id = new Guid("C10D0684-D25F-4E5E-A347-16F85DB7BFBF") });
        Console.WriteLine("New message received: {0}", message.UserSystemId);
    }
}

If the first line in the handler method is commented out, the message is processed only once.

I have found some posts/threads on the web (including StackOverflow) which talk about similar issues, notably http://tech.groups.yahoo.com/group/nservicebus/message/5977 and Anyone using Ninject 2.0 as the nServiceBus ObjectBuilder? - but I haven't had any success in making my problem go away.

I'd be most obliged for any help. I'm a n00b at NServiceBus!

Upvotes: 1

Views: 2819

Answers (2)

Scott Millett
Scott Millett

Reputation: 91

NServiceBus isn't handling it multiple times by default it will retry 5 times if an exception occurs, you can set this in a config file. Have you got distributed transactions turned on? Because you are committing to a database and you have an open transaction (the queue transaction) when you open another transaction it will try and upgrade it to a distributed transaction, I think that may be the issue. Have you run with the console app? You should see some out put on there.

Upvotes: 4

John Simons
John Simons

Reputation: 4288

I would recommend wrapping the body of the Handle method in a try/catch and add a break point to the catch and see what is wrong. Once you work it out, remove the try/catch.

Upvotes: 1

Related Questions