0

My project is upgrading to use nservice bus version 7. One of the handler is getting executed concurrently. After analysis found that there is a behavior code written and getting executed after handler election. Then next the handler will get executed.This will executed in loop and not getting ended.

public class GatewayPublishBehavior : Behavior<IIncomingLogicalMessageContext>
{

  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.RetrieveAndGetSendOptions(context));
  }
}

Above is the behavior code. Not sure why the handler getting executed multiple times.

Dennis van der Stelt
  • 2,203
  • 16
  • 22
  • 2
    It doesn't look like you posted the full code of the behaviour. – gnud Mar 06 '20 at 00:18
  • What @gnud said. The doco for behaviors can be found here : https://docs.particular.net/nservicebus/pipeline/manipulate-with-behaviors – Dennis van der Stelt Mar 06 '20 at 07:47
  • If you do `await next()` properly and only once, and no other weird behavior, it should not just execute any handler two times for the same message instance. – Dennis van der Stelt Mar 06 '20 at 07:48
  • @gnud the complete code is as below. public override async Task Invoke(IIncomingLogicalMessageContext context, Func 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.RetrieveAndGetSendOptions(context)); } – DealZaap India Mar 06 '20 at 10:01
  • @DennisvanderStelt the complete code is as below. public override async Task Invoke(IIncomingLogicalMessageContext context, Func 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.RetrieveAndGetSendOptions(context)); } – DealZaap India Mar 06 '20 at 10:01

2 Answers2

2

It's exactly like this code

public void Whatever()
{
  Whatever();
}

An endless loop. Just remove the Publish. Why did you add that line? Do you like duplicates? Because you posted the exact same question twice as well. Trying to create a recursive loop inside StackOverflow?

Dennis van der Stelt
  • 2,203
  • 16
  • 22
0

Every task need to return something.

return Task.CompletedTask;

Just add the upper given code at the bottom of the Handler. It will solve you problem

Valerij Dobler
  • 1,848
  • 15
  • 25
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 24 '22 at 07:47