带有JsonIgnoreAttribute的属性的Json.NET JsonSerializationException

时间:2017-12-20 14:04:51

标签: json json.net

我收到了例外:

Newtonsoft.Json.JsonSerializationException:使用类型'DbGet.Service.Auth.AuthUserRegisterSagaData'检测到自引用循环。路径'Tenant.CanRollback'。

    internal class AuthUserRegisterSagaData : ContainSagaData
{
    public AuthUserRegisterSagaData()
    {
        Tenant = new MultipartEntitySagaData<TenantEntity>();
        TenantUser = new MultipartEntitySagaData<TenantUserEntity>();
        Workspace = new MultipartEntitySagaData<WorkspaceEntity>();
        Workspace.CanRollback = () => true;
        TenantUser.CanRollback = () => Workspace.RollbackCommandStatus == CommandStatusEnum.Completed;
        Tenant.CanRollback = () => TenantUser.RollbackCommandStatus == CommandStatusEnum.Completed;
    }

    public string Email { get; set; }

    /// <summary>
    /// Is User Entity saved
    /// </summary>
    public bool IsUserCreated { get; set; }

    public Guid UserId { get; set; }

    public IdentityUserEntity Entity { get; set; }

    public MultipartEntitySagaData<TenantEntity> Tenant { get; set; }

    public MultipartEntitySagaData<TenantUserEntity> TenantUser { get; set; }

    public MultipartEntitySagaData<WorkspaceEntity> Workspace { get; set; }
}

public class MultipartEntitySagaData<TEntity>: MultipartSagaData
{
    public TEntity Entity { get; set; }      
}

public class MultipartSagaData
{
    public CommandStatusEnum CommandStatus { get; set; }

    public CommandStatusEnum RollbackCommandStatus { get; set; }

    [JsonIgnore]
    public Func<bool> CanRollback { get; set; }
}

我不明白为什么,因为我将此属性标记为不可序列化。

1 个答案:

答案 0 :(得分:0)

您能否显示一些关于序列化的代码?是3级还是更少?如果您还在序列化内部类,那么我认为Tenant(将被序列化)在构造函数中给出了问题。

这是一个带有相同错误的链接。希望它会有所帮助。 Same error