MediatR.ParallelPublisher is an extension for MediatR that enables parallel and fire-and-forget publishing of notifications. The library aims to improve performance and responsiveness by executing notification handlers concurrently, while also providing exception handling mechanisms for notification processing.
- Parallel execution of notification handlers
- Fire-and-forget notifications
- Customizable exception handling
Install MediatR.ParallelPublisher via NuGet Package Manager or by running the following command:
dotnet add package MediatR.ParallelPublisher
After installing the package, register the parallel publisher in your dependency injection container:
services.AddMediatR(config =>
{
config.UseParallelNotificationPublisher(options =>
{
// Register custom exception handlers (optional)
options.RegisterExceptionHandler<MyCustomExceptionHandler>();
});
});
To make a notification fire-and-forget, either implement the IFireAndForgetNotification
interface or use the
FireAndForgetNotificationAttribute
:
// Using the IFireAndForgetNotification interface
public class MyNotification : INotification, IFireAndForgetNotification
{
// ...
}
// Using the FireAndForgetNotificationAttribute
[FireAndForgetNotification]
public class MyOtherNotification : INotification
{
// ...
}
Implement your notification handlers as usual, and they will be executed in parallel or fire-and-forget mode, depending on the notification type:
public class MyNotificationHandler : INotificationHandler<MyNotification>
{
public Task Handle(MyNotification notification, CancellationToken cancellationToken)
{
// Your handler implementation
}
}
For custom exception handling, implement the INotificationExceptionHandler
interface:
public class MyCustomExceptionHandler : INotificationExceptionHandler
{
public Task HandleAsync(INotification notification, NotificationException exception)
{
// Handle the exception for the specified notification
}
}
MediatR.ParallelPublisher is released under the Apache License. See the LICENSE file for details.
Contributions are welcome! Feel free to submit issues, feature requests, or pull requests.