c# - System.InvalidOperationException: A circular reference was detected -


system.invalidoperationexception: there error generating xml document. ---> system.invalidoperationexception: circular reference detected while serializing object of type projectnetwork.models.branch.    @ system.xml.serialization.xmlserializationwriter.writestartelement(string name, string ns, object o, boolean writeprefixed, xmlserializernamespaces xmlns)    @ microsoft.xml.serialization.generatedassembly.xmlserializationwriter1.write7_branch(string n, string ns, branch o, boolean isnullable, boolean needtype)    @ microsoft.xml.serialization.generatedassembly.xmlserializationwriter1.write6_company(string n, string ns, company o, boolean isnullable, boolean needtype)    @ microsoft.xml.serialization.generatedassembly.xmlserializationwriter1.write7_branch(string n, string ns, branch o, boolean isnullable, boolean needtype)    @ microsoft.xml.serialization.generatedassembly.xmlserializationwriter1.write8_branch(object o)    @ system.xml.serialization.xmlserializer.serialize(xmlwriter xmlwriter, object o, xmlserializernamespaces namespaces, string encodingstyle, string id)    --- end of inner exception stack trace ---    @ system.xml.serialization.xmlserializer.serialize(xmlwriter xmlwriter, object o, xmlserializernamespaces namespaces, string encodingstyle, string id)    @ system.xml.serialization.xmlserializer.serialize(textwriter textwriter, object o, xmlserializernamespaces namespaces)    @ system.web.services.protocols.xmlreturnwriter.write(httpresponse response, stream outputstream, object returnvalue)    @ system.web.services.protocols.webservicehandler.writereturns(object[] returnvalues)    @ system.web.services.protocols.webservicehandler.invoke() 

a few hours ago , far not solve problemو tired , last chance before became crazy :) can find solved problem ? i'm trying return object of type branch child include object of parent company.

  • branch class

    public partial class branch {     public branch()     {         this.customers = new list<customer>();     }          public int branchid { get; set; }         public int companyid { get; set; }         public string branchname { get; set; }         public string branchshortname { get; set; }         public string branchaddress { get; set; }         public string branchphone { get; set; }         public string branchemail { get; set; }         public string branchfax { get; set; }         public nullable<float> branchlimit { get; set; }         public bool recordstate { get; set; }         public virtual company company { get; set; }             } 
  • company class

    public partial class company {     public company()     {         this.branches = new list<branch>();     }      public int companyid { get; set; }     public string companyname { get; set; }     public string companyshortname { get; set; }     public string companyaddress { get; set; }     public string companyphone { get; set; }     public string companyemail { get; set; }     public string companyfax { get; set; }     public bool recordstate { get; set; }     public virtual list<branch> branches { get; set; } } 
  • newdbcontext class

    using system.data.entity; using system.data.entity.infrastructure; using projectnetwork.models.mapping;  namespace projectnetwork.models { public partial class newdbcontext : dbcontext {     static newdbcontext()     {         database.setinitializer<newdbcontext>(null);     }      public newdbcontext()         : base("name=newdbcontext")     {         this.configuration.lazyloadingenabled   = false;         this.configuration.proxycreationenabled = false;     }      public dbset<branch> branches { get; set; }     public dbset<company> companies { get; set; }      protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         modelbuilder.configurations.add(new branchmap());         modelbuilder.configurations.add(new companymap());               } } } 
  • call method

        [webmethod]     public branch getallcustomers()     {         branch  br = null;          using (var db = new newdbcontext())         {             br = db.branches                 .where(d => d.companyid == 1)                 .include(c => c.company)                 .firstordefault<branch>();                                 return br;          } 

sorry bad english

you can try adding jsonignore attribute on class stop being rendered, if using newtonsoft.json

public partial class company {     public company()     {         this.branches = new list<branch>();     }      ....      [jsonignore]     public virtual list<branch> branches { get; set; } 

}


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -