4
var response = SaveOrderInDB();
OrderCreatedEvent orderCreatedEvent = new OrderCreatedEvent(x, y, z);            
_requestRouter.Publish(orderCreatedEvent);
return response;

By MediatR docs the notifications is "Fire and forget" feature. I do not use await since I want to return immediately "response" object to client Angular app after notification was published. However when I put breakpoint in notification handler I see in Chrom dev tools that request still in pending status, waits for notification to finish.

 public Task Handle(OrderCreatedEvent notification, CancellationToken cancellationToken)
    {
        return Task.CompletedTask; // Breakpoint is here 
    }

How can I not wait for notifications to finish? enter image description here

Nick
  • 423
  • 4
  • 19
  • Isn't the break-point stopping it from returning task completed? – David Edel Dec 03 '19 at 17:35
  • @DavidEdel yes,it is. Maybe I was not clear enough. My question is how to publish MediatrR notifications without waiting for them to complete like it is on screenshot. – Nick Dec 03 '19 at 18:10

3 Answers3

2

As mentioned in the comments, the breakpoint is preventing them from completing.

If you don't believe this, change your NotificationHandler to something like:

public async Task Handle(OrderCreatedEvent notification, CancellationToken cancellationToken)
{
    await Task.Delay(5000);

    Console.Write("Done."); //put a breakpoint here
}

Put a breakpoint on the Console.Write method, then run your application, and call your endpoint.

You'll see the response isn't pending, and after 5 seconds, your breakpoint is hit.
(or "Done." is written to the console if you didn't set a breakpoint)

Alex
  • 37,502
  • 51
  • 204
  • 332
2

MediatR's Notifications aren't "Fire and forget" by default. The docs state under the heading "Publish strategies".

The default implementation of Publish loops through the notification handlers and awaits each one. This ensures each handler is run after one another.

If you want to have notifications not be awaited on you will have to override the behaviour of the PublishCore method in the mediator.cs class. The documentation mentions this can be done under the same header above and points to some sample code under MediatR.Examples.PublishStrategies. Have a look at the method ParallelNoWait in Publisher.cs in that sample to see how it works.

  • I did had a look at it but it was not clear for me how to add those publishing strategies to existing project with MediatR. I created a post but did not receive answer I can accept : https://stackoverflow.com/questions/59320296/how-to-add-mediatr-publishstrategy-to-existing-project Could you answer it maybe? – Nick Feb 12 '20 at 09:58
0
builder.Services.AddMediatR(_ =>
{
    _.RegisterServicesFromAssemblies(Assembly.GetExecutingAssembly());
    _.NotificationPublisher = new TaskWhenAllPublisher();
    _.NotificationPublisherType = typeof(TaskWhenAllPublisher);
});
T. Czubaszek
  • 91
  • 1
  • 7
  • 1
    Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. **Would you kindly [edit] your answer to include additional details for the benefit of the community?** – Jeremy Caney Jun 24 '23 at 00:04