Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Created July 17, 2023 09:32
Show Gist options
  • Select an option

  • Save ramonsmits/0cd03467a1aae03f126712456c8e9bf0 to your computer and use it in GitHub Desktop.

Select an option

Save ramonsmits/0cd03467a1aae03f126712456c8e9bf0 to your computer and use it in GitHub Desktop.
NServiceBus 6+ behavior to run the incoming message
// If all handlers are compute bound or IO isn't happening early it can result in less concurrent processing as depending on the
// transport and it being full async and no IO is happening message processing could be sequential. This behavior will immediately
// do a Task.Run
//
// Usage:
// endpointConfiguration.Pipeline.Register(new TaskRunBehavior(), nameof(TaskRunBehavior));
//
class TaskRunBehavior : IBehavior<IIncomingPhysicalMessageContext, IIncomingPhysicalMessageContext>
{
public Task Invoke(IIncomingPhysicalMessageContext context, Func<IIncomingPhysicalMessageContext, Task> next)
{
return Task.Run(() => next(context), context.CancellationToken);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment