gpt4 book ai didi

c# - 在 StackService.Redis 客户端方法 Store() 上导致 stackoverflow 的递归对象

转载 作者:IT王子 更新时间:2023-10-29 06:15:59 24 4
gpt4 key购买 nike

我有两个 POCO 类(帐户和发票),如您所见(下面是这些类的模型)它们是递归的。

当我传入一个设置了帐户属性的发票对象,然后尝试使用 redis 客户端存储它时,它会由于递归而导致堆栈溢出。以下是我如何调用电话的示例。

CachingService.Store<Invoice>(invoiceObj);


public class CachingService {
// ....
public static void Store<T>(T obj)
{
using (var client = _redisClientsManager.GetClient())
{
var typedClient = client.GetTypedClient<T>();
typedClient.Store(obj);
}
}
}

我的 POCO 类示例:

public class Account
{
public string Name { set; get; }
public bool IsActive { set; get; }

public virtual ICollection<Invoice> Invoices { set; get; }
}

public class Invoice
{
public bool IsPaid { set; get; }
public DateTime? LastSent { set; get; }
public int AccountId { set; get; }

public virtual Account Account { set; get; }
}

最佳答案

大多数序列化程序(包括 ServiceStack)不支持循环引用。这是设计 DTO 时的主要反模式。

要解决这个问题,你想告诉 ServiceStack.Text's serializers忽略序列化的该属性,您可以使用 [IgnoreDataMember] 属性或更改它以使其不是公共(public)属性或将其更改为方法。

关于c# - 在 StackService.Redis 客户端方法 Store() 上导致 stackoverflow 的递归对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11911876/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com