首页 > 解决方案 > Hangfire、EF dbContext 和 JSON 参考循环

问题描述

我想使用 Hangfire 从我的 ASP.net Core API 运行具有数据库操作的后台任务。

我安装了 Hangfire nuget 并将 SQL dbConnection 设置在Startup.cs

GlobalConfiguration.Configuration.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection"));

当我现在想开始一项后台工作时:

var jobId = BackgroundJob.Enqueue(() => RebuildVersionHistoryCacheAsync(statisticContext));

我收到如下错误:

JsonSerializationException: Self referencing loop detected for property 'Context'  ...

我都尝试了,将全局序列化程序设置设置为忽略循环并设置hangfire设置做同样的事情:

services.AddControllers().AddNewtonsoftJson(options =>
    {
      options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });

...

services.AddHangfire((sp, config) =>
     {
       config.UseSerializerSettings(new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
     });

没有效果。

Hangfire 的配置有什么我想念的吗?或者,Hangfire 在 BackgroundJobs 中结合 EF Core 数据库上下文是否存在基本问题?

标签: c#entity-frameworkasp.net-corehangfireef-core-3.1

解决方案


推荐阅读