0

我正在使用 RedisClientManager 并且我正在尝试设置对象时在 mscorlib.dll 错误中发生“System.StackOverflowException”类型的未处理异常

client.Set<ApplicationUser>(user.Id, user);

和用户:

public class ApplicationUser : IdentityUser
{        
    public string Name { get; set; }
    public string Surname { get; set; }
    public DateTime? BirthDay { get; set; }
    public int BirthPlace { get; set; }
    public int TeamId { get; set; }
    public int AvatarId { get; set; }
    public string Address { get; set; }
    public DateTime RegisterationDate { get; set; }
    public DateTime CodeSendDate { get; set; }
    public string ActivationCode { get; set; }
    public string PasswordResetToken { get; set; }
    public string FacebookAvatar { get; set; }
    public string FacebookId { get; set; }
    public bool UseFacebookAvatar { get; set; }
    public string IpAddress { get; set; }

    public virtual Avatar Avatar { get; set; }

    public ApplicationUser()
    {
        this.Coupons = new HashSet<Coupon>();
    }

    [JsonIgnore]
    public virtual ICollection<Coupon> Coupons { get; set; }
}

序列化ApplicationUser时发生错误,我尝试添加[JsonIgnore]ICollection,因为嵌套循环,(优惠券包含用户)我找不到什么问题?

4

1 回答 1

0

我找到了适合我的解决方案。ApplicationUser 对象有 Coupon 并且 Coupon 有 ApplicationUser (多对多),所以序列化进入无限循环。

我加[IgnoreDataMember]

public class ApplicationUser : IdentityUser
{        
    public string Name { get; set; }
    public string Surname { get; set; }
    public DateTime? BirthDay { get; set; }
    public int BirthPlace { get; set; }
    public int TeamId { get; set; }
    public int AvatarId { get; set; }
    public string Address { get; set; }        
    public DateTime RegisterationDate { get; set; }        
    public DateTime CodeSendDate { get; set; }
    public string ActivationCode { get; set; }
    public string PasswordResetToken { get; set; }
    public string FacebookAvatar { get; set; }
    public string FacebookId { get; set; }
    public bool UseFacebookAvatar { get; set; }
    public string IpAddress { get; set; }        

    public virtual Avatar Avatar { get; set; }


    public ApplicationUser()
    {
        this.Coupons = new HashSet<Coupon>();
    }

    [IgnoreDataMember]
    public virtual ICollection<Coupon> Coupons { get; set; }
}

现在我可以忽略 Coupon 属性,因此 Servis.Stack redisClientManager 可以序列化对象。

于 2015-02-20T09:56:37.050 回答