I'm implementing a .NET 8 worker service with Quartz.NET & MediatR. The flow of which is:
Gets triggered after certain intervals (defined by cron expression) by Quartz.NET job scheduler --> Call an external RESTful API --> Publish the API response through MediatR --> Repeat
There is a MediatR notification handler, it is supposed to receive the message and store it in AWS DynamoDB.
The code of the handler is roughly:
using MediatR;
namespace TestApp;
public class TestNotificationHandler : INotificationHandler<NotificationData>, IDisposable
{
public async Task Handle(NotificationData notification, CancellationToken cancellationToken)
{
// save notification to DynamoDB
await Task.CompletedTask;
}
public void Dispose()
{
// dispose DynamonDB context
}
}
The issue is:
The Quartz.NET job trigger is finishing execution before the notification could be handled (i.e. saved to DynamoDB) and so the Dispose() method is getting invoked.
Is this the correct behaviour? What needs to be changed if I want to make sure the handlers run independently of the Quartz.NET job trigger?