.net - Serialization Circular exception caused by self-referencing Read-only property -


when trying return object json asp.net 3.5sp1 webservice (not wcf, classic asp.net webservice scriptservice attribute), have "a circular reference detected while serializing object of type 'geo.bound'" error, caused self-referencing read-only property :

simplified code :

namespace geo <datacontract(namespace:="geo", isreference:=true)> _ public class bound   <datamember(name:="sw", isrequired:=false)> _  public southwestcoord double    public sub new()   southwestcoord = 1.5#  end sub   <ignoredatamember()> _  public readonly property bds() bound      return me   end  end property  end class end namespace 
  • i want keep read-only property because it's used implementing interface.
  • adding "isreference:=true" attribute bound class changes nothing.
  • if use datacontractjsonserializer (outside context of webservice, exemple : link text), works , have correct json.
  • if remove "bds" read-only property works !!

i don't understand why ! it's readonly property, without datamember attribute, ignoredatamember attribute, it's not supposed serialized !

how keep "bds" property, , rid of circular reference exception ?

thanks !

here example, works (sorry c#)

  1. defined class:

    [datacontract(namespace = "geo")] public class bound {     [ignoredatamember]     public bound { { return this; } }      [datamember]    public string name { get; set; } } 
  2. created service interface (and implementation)

    [servicecontract] public interface iservice1 {     [operationcontract]     [webinvoke(method = "get", responseformat = webmessageformat.json)]     bound dowork(); }   public class service1 : iservice1 {     public bound dowork()     {         return new bound { name = "test name" };     } } 
  3. edited system.servicemodel part of app.config

    <behaviors>   <endpointbehaviors>     <behavior name="endbeh">       <enablewebscript/>     </behavior>   </endpointbehaviors> </behaviors> <services>   <service name="jsonserializationtest.service1">     <endpoint address="" binding="webhttpbinding" behaviorconfiguration="endbeh" contract="jsonserializationtest.iservice1" />     <host>       <baseaddresses>         <add baseaddress="http://localhost:8732/design_time_addresses/jsonserializationtest/service1/"/>       </baseaddresses>     </host>   </service> </services> 
  4. started service host in program.cs

    using (var sh = new servicehost(typeof(service1))) {     sh.open();     console.writeline("opened");     console.readline(); } 

started program, opened browser, typed http://localhost:8732/design_time_addresses/jsonserializationtest/service1/dowork , recieved json-ed test object:

{"d":{"__type":"bound:geo","name":"test name"}} 

ps: webinvokeattribute located in system.servicemodel.web.dll assembly.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -