1

Facing issues in handler of Nservicebus. Handler executing multiple times. please find the code below. Facing problem after running this code the handler in letthandler class is getting executed multiple time.

Lett handler class

public Task Handle(Message message, IMessageHandlerContext context)
        {
            Log.Info($"Processing started");
            return Task.CompletedTask;
        }

Behaviuor class

public override async Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
        {
            //// custom logic before calling the next step in the pipeline.

            await next().ConfigureAwait(false);

            // custom logic after all inner steps in the pipeline completed.

            await context.Publish(context.Message.Instance, this.RetrieveOptions(context)).ConfigureAwait(false);
        }

        private PublishOptions RetrieveOptions(IIncomingLogicalMessageContext context)
        {
            var publishOptions = new PublishOptions();
            publishOptions.RequireImmediateDispatch();

            string requestPoolCounter;
            if (context.MessageHeaders.TryGetValue(Header.SentPoolCounter, out requestPoolCounter))
            {
                publishOptions.SetHeader(Header.SentPoolCounter, requestPoolCounter);
            }

            string primaryKey;
            if (context.MessageHeaders.TryGetValue(Header.PrimaryKey, out primaryKey))
            {
                publishOptions.SetHeader(Header.PrimaryKey, primaryKey);
            }

            string crmEntity;
            if (context.MessageHeaders.TryGetValue(Header.EntityType, out crmEntity))
            {
                publishOptions.SetHeader(Header.EntityType, crmEntity);
            }

            return publishOptions;
        }

HandlersConnector class

public override Task Invoke(IIncomingLogicalMessageContext context, Func<IInvokeHandlerContext, Task> stage)
        {
            context.MessageHandled = true;
            return Task.CompletedTask;
        }

It seems that After executing the code
await context.Publish(context.Message.Instance, this.RetrieveOptions(context)).ConfigureAwait(false); in Behaviour class the lett handler handle getting executed and that loop will continue. Not sure what is the issue. Recently project reference updated from 4 to 7.2 Nservicebus. Any suggestions?

  • 1
    Does this answer your question? [Nservice Bus handler trigger issue](https://stackoverflow.com/questions/60553781/nservice-bus-handler-trigger-issue) – Dennis van der Stelt Mar 07 '20 at 12:57

0 Answers0