NHibernate。LazyInitializationException异常发生在Newtonsoft.Json.dll

时间:2021-06-27 05:38:10

I cannot serialize then immediately deserialize a large object without issues. I followed advice from: JSON.NET and nHibernate Lazy Loading of Collections and JSON.Net Serialization of NHibernate Proxies (NH 3.3.2.4000) to get Json.Net working with my legacy system. Despite trying the suggestions and in various combinations i have had no success. Here are the settings that make most sense to me as i understand them all.

我不能立即序列化一个没有问题的大对象。我听从了以下建议:JSON。NET和nHibernate延迟加载集合和JSON。Net序列化NHibernate代理(NH 3.3.2.4000)以获得Json。Net使用我的遗留系统。尽管我尝试了各种各样的建议,但都没有成功。以下是我最理解的设置。

Converter:

转换器:

protected override List<MemberInfo> GetSerializableMembers(Type objectType)
{
    if (typeof(INHibernateProxy).IsAssignableFrom(objectType))
    {
        return base.GetSerializableMembers(objectType.BaseType);
    }
    else
    {
        return base.GetSerializableMembers(objectType);
    }
}

Main Code:

主要代码:

    var jsonSerializerSettings = new JsonSerializerSettings
    {
        ContractResolver = new NHibernateContractResolver(),
        PreserveReferencesHandling = PreserveReferencesHandling.All,
        TypeNameHandling = TypeNameHandling.Auto,
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        ObjectCreationHandling = ObjectCreationHandling.Replace
    };

    string serialisedEnquiry = JsonConvert.SerializeObject(enquiry, Formatting.Indented, jsonSerializerSettings);

    Enquiry enq = JsonConvert.DeserializeObject<Enquiry>(serialisedEnquiry, jsonSerializerSettings);

I have another SO question open which may be of note: JSON.net null property Although as you will see in the last comment, i believe i have solved it. I am just waiting to solve this problem to confirm the other is fixed and not simply buried/replaced by this new error.

我还有另一个SO问题需要注意:JSON.net空属性,尽管正如你在上一个评论中看到的,我相信我已经解决了它。我只是在等待解决这个问题,以确认另一个问题是固定的,而不是简单地被这个新的错误所掩盖/替换。

Fluent NHibernate 1.4.0.0

连贯NHibernate 1.4.0.0

NHibernate 3.3.1.4000

NHibernate 3.3.1.4000

Netwonsoft.Json 6.0.0.0

Netwonsoft。Json 6.0.0.0

EDIT:

编辑:

My exception is:

我的例外是:

An unhandled exception of type 'NHibernate.LazyInitializationException' occurred in Newtonsoft.Json.dll

类型为“NHibernate”的未处理异常。发生在Newtonsoft.Json.dll LazyInitializationException异常”

Additional information: Initializing[Unavailable#]-failed to lazily initialize a collection, no session or session was closed

附加信息:初始化[Unavailable#]-延迟初始化集合失败,没有关闭会话或会话

I have actually found this buried within my JSON:

实际上,我在JSON中找到了它:

$type : "NHibernate.Collection.Generic.PersistentGenericBag`1[[ComponentModel.Role, ComponentModel]], NHibernate"

类型:美元“NHibernate.Collection.Generic.PersistentGenericBag 1[[ComponentModel。角色,ComponentModel]],NHibernate”

I am not sure why when i have the NHibernate converter but it gives me a lead i can look into. I will post back if i find anything.

我不知道为什么当我有NHibernate转换器的时候,但是它给了我一个我可以研究的线索。如果我发现什么,我会把它寄回去。

1 个解决方案

#1


1  

I never found out exactly what was wrong but i happened across this configuration when i was playing with it and it worked.

我从来没有弄清楚到底是哪里出了问题,但是当我在玩的时候,我碰巧遇到了这个配置,它成功了。

    jsonSerializerSettings = new JsonSerializerSettings()
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
        PreserveReferencesHandling = PreserveReferencesHandling.Objects,
        ContractResolver = new NHibernateContractResolver(),
        NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
        TypeNameHandling = TypeNameHandling.Objects,
        //TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple,
        ObjectCreationHandling = ObjectCreationHandling.Replace
    };

NHibernateContractResolver class:

NHibernateContractResolver类:

public class NHibernateContractResolver : DefaultContractResolver
{
    protected override JsonContract CreateContract(Type objectType)
    {
        if (typeof(INHibernateProxy).IsAssignableFrom(objectType))
            return base.CreateContract(objectType.BaseType);
        else
            return base.CreateContract(objectType);
    }
}

#1


1  

I never found out exactly what was wrong but i happened across this configuration when i was playing with it and it worked.

我从来没有弄清楚到底是哪里出了问题,但是当我在玩的时候,我碰巧遇到了这个配置,它成功了。

    jsonSerializerSettings = new JsonSerializerSettings()
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
        PreserveReferencesHandling = PreserveReferencesHandling.Objects,
        ContractResolver = new NHibernateContractResolver(),
        NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore,
        TypeNameHandling = TypeNameHandling.Objects,
        //TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple,
        ObjectCreationHandling = ObjectCreationHandling.Replace
    };

NHibernateContractResolver class:

NHibernateContractResolver类:

public class NHibernateContractResolver : DefaultContractResolver
{
    protected override JsonContract CreateContract(Type objectType)
    {
        if (typeof(INHibernateProxy).IsAssignableFrom(objectType))
            return base.CreateContract(objectType.BaseType);
        else
            return base.CreateContract(objectType);
    }
}