I want to use Hangfire to run background tasks with database operations from my ASP.net Core API.
I installed the Hangfire nuget and set the SQL dbConnection in the Startup.cs:
GlobalConfiguration.Configuration.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection"));
When I now want to start a background job like:
var jobId = BackgroundJob.Enqueue(() => RebuildVersionHistoryCacheAsync(statisticContext));
I get an error like:
JsonSerializationException: Self referencing loop detected for property 'Context' ...
I tried both, set the global serializer settings to ignore loops and set the hangfire settings doing the same:
services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
...
services.AddHangfire((sp, config) =>
{
config.UseSerializerSettings(new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
});
With no effect.
Is there something I miss in the configuration of Hangfire? Or is there a base problem with Hangfire in combination of EF Core Database Contexts in BackgroundJobs?