Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate json proxy serialization with with ReferencesAny

I have the following mapping:

    public class TimeLineEntityMap : BaseEntityMap<TimeLineEntity>
    {
        public TimeLineEntityMap()
        {
            Table("time_line_entity");
            Map(x => x.Message);
            Map(x => x.ResearchId, "research_id");//.Cascade.All().Not.LazyLoad();
            ReferencesAny(x => x.EntityRef)
                .AddMetaValue<EmailEntity>(typeof(EmailEntity).Name)
                .AddMetaValue<UrlEntity>(typeof(UrlEntity).Name)
                .AddMetaValue<PhoneEntity>(typeof(PhoneEntity).Name)
                .EntityTypeColumn("entity_type")
                .IdentityType<long>()
                .EntityIdentifierColumn("entity_ref_id")
                .Not.LazyLoad();
        }
    }

when fetching from the database EntityRef comes as a proxy.

TimeLineEntity res = timeLineRepository.Find(x => x.Id == id);
JsonConvert.SerializeObject(res);

the JsonConvert is throwing:

Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'ManifestModule' with type 'System.Reflection.RuntimeModule'. Path 'Data[0].EntityRef._proxyFactoryInfo._getIdentifierMethod.Module.Assembly'.

this is my json settings:

     x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
     x.SerializerSettings.ContractResolver = new NHibernateContractResolver();
    public class NHibernateContractResolver : CamelCasePropertyNamesContractResolver
    {
        protected override JsonContract CreateContract(Type objectType)
        {
            if (typeof(NHibernate.Proxy.INHibernateProxy).IsAssignableFrom(objectType))
                return base.CreateContract(objectType.BaseType);
            else
                return base.CreateContract(objectType);
        }
    }
like image 760
SexyMF Avatar asked Sep 21 '19 08:09

SexyMF


1 Answers

Try to add [JsonIgnore] on your model class like this:

[JsonIgnore]
public class TimeLineEntity {

}

UPDATE To get serialization of it You should resolve circular reference of map.

Below links will help you.(May be your answer already existed here.)

JSON.NET Error Self referencing loop detected for type

Resolve circular references from JSON object

Stringify (convert to JSON) a JavaScript object with circular reference

http://blogs.microsoft.co.il/gilf/2011/10/17/avoiding-circular-reference-for-entity-in-json-serialization/

Good luck.

like image 85
Amir Christian Avatar answered Nov 15 '22 04:11

Amir Christian