.NET Core MediatR with Notification(Publish) and Behaviors

Maulik Patel
3 min readDec 16, 2022

--

Previous article we have seen command and query with working flow for adding product and retrieve product using CQRS + MediatR.

https://medium.com/@mlkpatel0/cqrs-and-mediatr-in-asp-net-core-web-api-ac0a68f52903

Now let’s explore it further for notification and behavior with MediatR.

MediatR notification (Events)

For Microservices architecture we required to publish events once operation successfully completed. Like Once order placed send email and send details to Order dispatch service.

We have seen one request handle by single handler. However, Single request can be handling by Multiple handler. Some independent operation needs to occur after some events.

For example

· Sending email

· Audit logs after records added or edited

Let’s create notification and handler. Add new folder called Notifications . It also referred as events. Add class with name ProductAddedNotification.

We created class ProductAddedNotification which implement INotification, with single property product.

Now we will create two handlers one for sending email and one for audit logs.

AuditLog Handler.

Implement INotificationHandler<ProductAddedNotification>, signifying it can handle that event.

Trigger the Notification

Open AddproductHandler and modify handler method for publish Product once saved in database.

Testing our Notifications

Here we can see logs from both handlers.

MediatR Behaviors

Instead of repeating this logic throughout in handler, we can use behaviors. Behaviors are very similar to .NET core middleware.

Let’s look at implementing a MediatR behavior that does logging for us .

MediatR pipeline behaviors were introduced in Version 3, It provide functionality to validate or logging logic before and after your command or query handlers execute. So handler no need to write repeated logic for logging or validation.

Creating our Behavior

Let’s add another solution folder “Behaviors”. Add class inside Behaviors folder LoggingBehavior.cs

· We first define a LoggingBehavior class, taking two types of parameters TRequest and TResponse, and implementing the IPipelineBehavior<TRequest, TResponse> interface. Simply put, this behavior can operate on any request.

  • We then implement the Handle method, logging before and after we call the next() delegate.

This logging handler can then be applied to any request, and will log output before and after it is handled.

Registering our Behavior

Let’s add line in program.cs

builder.Services.AddSingleton(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));

Notice that we are using the <,> notation to specify the behavior that can be used for any generic type parameters.

Test Behavior

Here we can see request and response both logged with behaviors.

Important things here we don’t need to modify existing requests or handlers. Similar we can add authorization and validation.

Further we can modify ProductAddedNotification class to publish events to Kafka, RabbitMQ or Azure bus.

Other services can read messaged and process information.

Code available at GitHub Repo: https://github.com/m-patel90/InvoiceService

Thanks for reading!

Kindly follow me for keep learning!

--

--

Maulik Patel

Full stack Software Engineer. C# | .NET core | MSSQL | Azure | Docker | Angular12+