0

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?

Konrad
  • 3,810
  • 7
  • 49
  • 77
  • 1
    Does this answer your question? [JSON.NET Error Self referencing loop detected for type](https://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop-detected-for-type) – Rao official Jul 16 '20 at 13:49
  • 1
    or this https://dotnetcoretutorials.com/2020/03/15/fixing-json-self-referencing-loop-exceptions/ – Rao official Jul 16 '20 at 13:50
  • Nothing of the mentioned hints worked - I rebuild it without Hangfire – Konrad Jul 22 '20 at 12:57

0 Answers0