【问题标题】:Self referencing loop while trying to use Hangfire尝试使用 Hangfire 时的自引用循环
【发布时间】:2020-03-17 20:51:04
【问题描述】:

我正在 ASP.NET Core 中开发一个 Web API,我们发现自己需要为大型批量插入操作运行后台任务。然而,我插入的模型包含来自.NET Topology SuiteGeometry 类型的属性。

public class GeometricData
{
    //...

    public Geometry Geometry { get; set; }
}

为了批量插入,我采用了一种我找到的方法here,它的性能非常好,但它的实现超出了这个问题的范围。尽管速度很快,但用户可能会一次性插入超过一百万条记录,因此我们决定将此处理移至后台任务。 Hangfire 扩展起初看起来可以为我们节省大量时间,但它似乎不能很好地处理 Geometry 类型。在下面的代码中,BackgroundTask 方法也可能是一个空方法:

public Task BulkInsert(IEnumerable<GeometricData> list)
{
    BackgroundJob.Enqueue(() => BackgroundTask(list));

    return Task.CompletedTask;
}   

Enqueue 的操作中将Geometry 列表作为参数传递给BackgroundTask 的行为将引发不幸的错误:

Self referencing loop detected for property 'CoordinateValue' with type 'NetTopologySuite.Geometries.Coordinate'. Path '[0].Geometry.Coordinates[0]'.

事实上,Coordinates(一个 NTS 类)确实引用了自己:

不知道他们为什么要这样做,但他们确实做到了。无论如何,到目前为止一切都很好,但是除非我设法找到解决方案(或者甚至可以解决这个问题),否则我将在从头开始实施后台工作人员时遇到一堆麻烦(我'将使用 Worker 服务,以防有人想知道)。有什么指点吗?

【问题讨论】:

  • Coordinates 不是 NTS 类别。它是 Geometry 的属性,其类型为 Coordinate[]。问题是为什么你的方法首先要访问它。
  • 盲测:您可以尝试自定义序列化程序,例如Hangfire.Common.JobHelper.SetSerializerSettings(new JsonSerializerSettings{Converters = new List&lt;JsonConverter&gt;() { new MyCoordinateJsonConverter()}});,其中MyCoordinateJsonConverterJsonConverter&lt;Coordinate&gt;,它负责自我引用
  • 不要在任务中运行 background.enqueue...hangfire 作业已经是任务

标签: c# gis asp.net-core-webapi hangfire nettopologysuite


【解决方案1】:

伙计,在几何和坐标上方添加 JsonIgnoreAttribute,另外:检查以下链接:newtonsoft.com/json/help/html/PropertyJsonIgnore.htm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多