Sponsor: Using RabbitMQ or Azure Service Bus in your .NET systems? Well, you could just use their SDKs and roll your own serialization, routing, outbox, retries, and telemetry. I mean, seriously, how hard could it be?

Best Practice
Method invocation (i.e. a job) is serialized during the background job creation process. Arguments are converted into JSON strings using the TypeConverter class. If you have complex entities and/or large objects; including arrays, it is better to place them into a database, and then pass only their identities to the background job. Instead of doing this:Consider doing this:public void Method(Entity entity) { }
public void Method(int entityId) { }
New Process
I liked my initial approach using extension methods and using the Hangfire’s JobActivator in order to construct the class that will be sending the Request to MediatR. Using the best practice the changes needed would be:- Pass a database connection factory to the Hangfire Configuration extension method (UseMediatr)
- Create a new MediatR extension method to pass a CommandId and the Request
- Insert a new record into a database with the CommandId and serialized Request
- Create a new method that will be invoked by Hangfire when only using the Command
- Select the record out of the database and deserialize the Request
- Send to MediatR
Hangfire Configuration
A database connection factory needs to be passed when configuration MediatR with Hangfire.Enqueue
Now in Enqueue a new command with or without a Command Id.Database
In my working code, I’m creating a table [MediatR.Request] in the Hangfire database automatically which is used to store the serialized commands. Since this is a example, it only contains the CommandId and the serialized command. However, it would likely make send to store the date created as well as when the date the command was invoked and completed by Hangfire.Demo
