Deserialization

am creating a base hangfire class which provides inheritance. to avoid including hangfire in all modules I pass the JobCancellation token to a method as an object to the BackgroundJob.Enqueue lamba. by default it cannot be deserialized. I saw a setting:

Hangfire.Common.JobHelper.SetSerializerSettings(new Newtonsoft.Json.JsonSerializerSettings
{
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All
});

when this is included, the token is properly deserialized/serialized but when the task is run, it fails every time with an SEHException

The method:
Hangfire.BackgroundJob.Enqueue(o => o.Execute(
new JobCancellationToken(false) as IJobCancellationToken,
parms));

BTW, IJobCancellationToken and JobCancellationToken behaves the same way.

If I take the setting out and serialize the token manually, it works…

please advise

use

Hangfire.BackgroundJob.Enqueue(o => o.Execute(
JobCancellationBatch.Null,
parms));

Instead. When instantiates the object, and calls the method, it will replace JobCancellationBatch.Null, with the actual token.

Also, I use this mechahism to Enqueue the job:

                    var createAnalysisJobId =
                        parent.Enqueue<IHangFireEngineBatch>(
                                x => x.CreatePortfolioAnalysisBatch(request, null, JobCancellationToken.Null));

Where IHangFireEngineBatch is an interface with the method defined.