proxy - JSON.Net Serialization of NHibernate Proxies (NH 3.3.2.4000) -
i'm still having continuous difficulties getting json.net , nhibernate play nicely together. namely, in getting json.net serialize proxied nhibernate object.
i've followed recommendations here, both accepted answer , ammendments, no dice.
the biggest problem above solution seems modern versions of nhibernate using inhibernateproxyproxy
interface create proxies (rather inhibernateproxy? can anyne else confirm this?), base class in case nhibernate.proxy.dynamicproxy.proxydummy
, reveals nothing underlying object when attempt create json constract using custom scontract resolver, ie:
protected override jsoncontract createcontract(type objecttype) { if (typeof(nhibernate.proxy.inhibernateproxy).isassignablefrom(objecttype)) return base.createcontract(objecttype.basetype); else return base.createcontract(objecttype); }
does have advice how deal inhibernateproxyproxy
effectively?
the full solution:
in global.asax.cs :
//define formatters var formatters = globalconfiguration.configuration.formatters; var jsonformatter = formatters.jsonformatter; var settings = jsonformatter.serializersettings; settings.formatting = formatting.indented; jsonformatter.serializersettings.referenceloophandling = newtonsoft.json.referenceloophandling.serialize; jsonformatter.serializersettings.preservereferenceshandling = newtonsoft.json.preservereferenceshandling.objects; jsonformatter.serializersettings.contractresolver = new nhibernatecontractresolver(); //------------//
and custom contract:
public class nhibernatecontractresolver : defaultcontractresolver { private static readonly memberinfo[] nhibernateproxyinterfacemembers = typeof(inhibernateproxy).getmembers(); protected override list<memberinfo> getserializablemembers(type objecttype) { var members = base.getserializablemembers(objecttype); members.removeall(memberinfo => (ismemberpartofnhibernateproxyinterface(memberinfo)) || (ismemberdynamicproxymixin(memberinfo)) || (ismembermarkedwithignoreattribute(memberinfo, objecttype)) || (ismemberinheritedfromproxysuperclass(memberinfo, objecttype))); var actualmemberinfos = new list<memberinfo>(); foreach (var memberinfo in members) { var infos = memberinfo.declaringtype.basetype.getmember(memberinfo.name); actualmemberinfos.add(infos.length == 0 ? memberinfo : infos[0]); } return actualmemberinfos; } private static bool ismemberdynamicproxymixin(memberinfo memberinfo) { return memberinfo.name == "__interceptors"; } private static bool ismemberinheritedfromproxysuperclass(memberinfo memberinfo, type objecttype) { return memberinfo.declaringtype.assembly == typeof(inhibernateproxy).assembly; } private static bool ismembermarkedwithignoreattribute(memberinfo memberinfo, type objecttype) { var infos = typeof(inhibernateproxy).isassignablefrom(objecttype) ? objecttype.basetype.getmember(memberinfo.name) : objecttype.getmember(memberinfo.name); return infos[0].getcustomattributes(typeof(jsonignoreattribute), true).length > 0; } private static bool ismemberpartofnhibernateproxyinterface(memberinfo memberinfo) { return array.exists(nhibernateproxyinterfacemembers, mi => memberinfo.name == mi.name); } protected override jsoncontract createcontract(type objecttype) { if (typeof(inhibernateproxy).isassignablefrom(objecttype)) { var otype = objecttype.getinterfaces().firstordefault(i => i.fullname.startswith("your.domain.namespace")); return otype != null ? base.createcontract(otype) : base.createcontract(objecttype.basetype); } return base.createcontract(objecttype); }
don't forget replace : "your.domain.namespace"
Comments
Post a Comment