1

In my Startup.cs I have AddJsonOptions with some options and some custom converters. It all works fine if I call API but if Hangfire fires a method that needs to serialize something where I used JsonConverter attribute, it doesn't recognize options I have in the Startup.cs for System.Text.Json.

Is there any way to configure System.Text.Json options so Hangfire recognizes them (mainly custom converters)? It's not an option for me to create JsonSerializerOptions as I'm not doing serialization myself myself.

Thanks

dbc
  • 104,963
  • 20
  • 228
  • 340
Mil
  • 113
  • 1
  • 11
  • 1
    According to [How to use system.text.json with hangfire?](https://discuss.hangfire.io/t/how-to-use-system-text-json-with-hangfire/9070) hangfire is still using Json.NET not System.Text.Json. – dbc Apr 10 '22 at 16:30
  • 1
    But even if it did, `AddJsonOptions()` only configures asp.net-core and does not affect direct calls to `JsonSerializer`, which does not support global settings at all. For confirmation see [Api proposal: Change JsonSerializerOptions default settings #31094](https://github.com/dotnet/runtime/issues/31094) and [How to globally set default options for System.Text.Json.JsonSerializer?](https://stackoverflow.com/a/58331912). – dbc Apr 10 '22 at 16:31
  • 1
    Hangfire would need to add a method to set `JsonSerializerOptions` similar to [`Hangfire.Common.JobHelper.SetSerializerSettings()`](https://api.hangfire.io/html/M_Hangfire_Common_JobHelper_SetSerializerSettings.htm). – dbc Apr 10 '22 at 16:34
  • Thank you @dbc for explanation. So my main problem is that my configuration for serializer options doesn't actually affect Hangfire or as you say, direct calls to JsonSerializer but only for asp.net-core instance. Well hopefully Hangfire will resolve that in the future, but for now I'll try and search for some kind of workaround to achieve what I need. Thanks again @dbc! – Mil Apr 10 '22 at 17:29

0 Answers0