Need help to solve JsonSerializationException and Hangfire.BackgroundJobClientException in hangfire while BackgroungJob.Enqueue

Hangfire.BackgroundJobClientException
HResult=0x80131500
Message=Background job creation failed. See inner exception for details.
Source=Hangfire.Core
StackTrace:
at Hangfire.BackgroundJobClient.Create(Job job, IState state)
at Freedom.StoreServer.FreedomDicomService.d__7.MoveNext() in D:\Innowave.Projects\Innowave.Core.Projects\FreedomStudyRouter\FreedomStudyRouter\Freedom.StoreServer\FreedomDicomService.cs:line 52

Inner Exception 1:
JsonSerializationException: Self referencing loop detected for property ‘Tag’ with type ‘Dicom.DicomTag’. Path ‘File.FileMetaInfo[0].Tag.DictionaryEntry’.

possible duplicate of this issue

I’m pretty sure this is a serialization issue.

Hangfire stores the method and the parameter in the database by serializing them. What probably happens is that in one of your parameter, there is a self referencing loop.

A way to prevent that is by preventing SelfReferencingLoop when serializing.

The way I do it is by changing the AddHangfire method :

services.AddHangfire((sp, config) =>
{
//Add the line below
config.UseSerializerSettings(new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
});

Thanx for your response ,
i have alreay tried this , at that point i receive different error . the error is:
Error : Hangfire.BackgroundJobClientException: Background job creation failed. See inner exception for details. —> Newtonsoft.Json.JsonSerializationException: Error getting value from ‘Preamble’ on ‘System.Text.ASCIIEncoding+ASCIIEncodingSealed’. —> System.InvalidProgramException: Cannot create boxed ByRef-like values.

Can you show me your BackgroungJob.Enqueue line?

I’m pretty sure you’re doing something like, Encoding.ASCII.Something()
my guess is that Json.Net can’t convert Encoding.ASCII

I would suggest putting your work into a method, and then calling that method inside the BackgroungJob.Enqueue, that way, json.net won’t have to serialize anything except your method name and assembly.