CommandScheduler 1.0.3
See the version list below for details.
dotnet add package CommandScheduler --version 1.0.3
NuGet\Install-Package CommandScheduler -Version 1.0.3
<PackageReference Include="CommandScheduler" Version="1.0.3" />
paket add CommandScheduler --version 1.0.3
#r "nuget: CommandScheduler, 1.0.3"
// Install CommandScheduler as a Cake Addin #addin nuget:?package=CommandScheduler&version=1.0.3 // Install CommandScheduler as a Cake Tool #tool nuget:?package=CommandScheduler&version=1.0.3
Integrate MediatR with Hangfire
Decoupling CQRS Commands
In a tightly coupled architecture, the application must wait for the database server to respond before it can finish a transaction. This design has the potential to create performance bottlenecks in both synchronous and asynchronous applications. Should the database server slow down due to poor tuning or hardware issues, the application will slow. Should the database stop responding or crash, the application will potentially crash as well.
graph LR
A[Application] -- Tightly coupled application --> B((Database))
By decoupling the database from the application, a loosely coupled architecture is created. In this architecture, Hangfire, as messaging-oriented middleware, acts as an intermediary for the data prior to some action being taken with it in the database. A consumer application picks up the data from the Hangfire server, performing the database action.
In this model, should a database need to be taken offline for maintenance, or
should the write workload become too heavy, you can throttle the consumer application or stop it. Until the consumer is able to receive the message, the data will persist.
graph LR
A[Application]-->B(Hangfire)
B-->C((Database));
To schedule your commands, execute them parallel with the retry option and monitor them. Hangfire
gives you all these kinds of features but You have to have public
method which You have to pass to Hangifre
method (for example BackgroundJob.Enqueue
). This is a problem – with mediator pattern You cannot pass public method of the handler because You have decoupled it from the invoker. So You need a special way to integrate MediatR
with Hangfire
without affecting basic assumptions.
I presented the way of processing commands asynchronously using MediatR
and Hangfire
. With this approach we have:
- Decoupled invokers and handlers of commands.
- Scheduling commands mechanism.
- Invoker and handler of command may be other processes.
- Command execution monitoring.
- Command execution retries mechanism.
These benefits are very important during development using an eventual consistency approach. We have more control over commands processing and we can react quickly if a problem will appear.
For this, In HSP.Infrastructure.Toolkits.CommandScheduler
we defined 4 classes:
CommandsScheduler
serializes commands and sends them toHangfire
.CommandsExecutor
responds toHangfire
jobs execution, deserializes commands and sends them to handlers usingMediatR
.MediatorSerializedObject
wrapper class for serialized/deserialized commands with additional properties – command type and additional description.MediatorExtension
extension methods forIMediator
public class MyMController : Controller
{
private IMediator _mediator;
public MyMicroserviceController(IMediator mediator)
{
_mediator = mediator;
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] TestCommand testCommand)
{
_mediator.Enqueue(testCommand);
return Ok();
}
}
And our commands are scheduled, invoked and monitored by Hangfire
.
Download source code
CommandScheduler is available on GitHub. https://github.com/AliBayatGH/CommandScheduler
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Hangfire (>= 1.7.7)
- MediatR (>= 7.0.0)
- Newtonsoft.Json (>= 12.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.