Handler multiple time execution issue

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?

What are you trying to accomplish with your behavior class? Are the original incoming messages commands or events?

It looks like you are re-publishing every message after it has been handled. The endpoint has a handler for messages of the published message type, so it is automatically subscribed to these messages. It will get a new copy of the message delivered again and the entire process will repeat.

This is a duplicate of these StackOverflow posts

I’ve included them here for reference for future readers

I believe it should work. Thanks for the updates.

@MikeMinutillo @Dennis
As you can see it’s modifing the context header before republishing in behavior class. Actually it should hit only once. Is there any option to modify the header before hitting the handler? After modification of header it’s should hit handler only once.

Are you trying to forward messages to somewhere else? As described in this sample?