Hangfire is one of the easiest ways to embed background processing in your .NET (Core) applications. It does all the hard work for you so you can focus on the application logic itself.
Scheduling a recurring job is as easy as using this oneliner:
As you can see background jobs in Hangfire look like regular method calls. Hangfire will collect and serialize the method information using the Newtonsoft.Json package and store the resulting JSON.
In real life applications I typically need to pass some arguments to the background job which is supported as well:
In the code above the Message
object is serialized as well. Although this is correctly serialized and stored by Hangfire, trouble arrives when this scheduled task is actually invoked. Hangfire will deserialize our Message
object but the private properties will remain Null
.
If you don’t mind having a dependency on Newtonsoft.Json, you can fix it by adding a [JsonProperty]
attribute:
Or the option that I prefer is to update the Hangfire serialization configuration: to support non public members: