We are leaving Russia. We are against aggression and war with Ukraine. It is a tragedy for our nations, it is a nightmare

How to use system.text.json with hangfire?

Here is a situation…
Hangfire uses Newtonsoft.json as serializer and deserializer and .NET has recommended to use system.text.json as serializer and deserializer…

now here is a situation i have dynamic and object type objects that need to be converted

so when job is enqueued in hangfire it is and executes it convert object/dynamics to JObject of (Newtonsoft.json) but i want to convert it to system.text.json.JElement.

In order to do such a thing, I think you would need to serialize your object using System.Text.Json and upload it in a separate storage system. That way, you could enqueue a message using Hangfire that reference the ID of the externally stored object.

That’s actually what I’ve done in this week. I built an abstraction over Hangfire that automatically serialize (using System.Text.Json) any message to Azure Blob Storage. On the job execution side, the blob is downloaded and the corresponding object handler is getting called to continue the job execution.

Having your job parameters (or message) stored in another system offers actually several benefits:

  • You decide how and where your message is serialized
  • You’ll use less space in the Hangfire storage, making the whole system faster
  • There will no longer be a limit to the size of the message
  • There’s less chances that someone see PII from your users in the Hangfire dashboard
  • If needed, inspecting/modifying the externally stored JSON object is easier than doing it in the Hangfire database