Marina
1/21/2003 7:37:00 PM
All serializable classes need to have a default constructor. Also, only
public members will be serialized.
"Nicolas Mayoraz" <nm01@dkweb.ch> wrote in message
news:1f9a8c6.0301210703.7ead352d@posting.google.com...
> I wrote a WebService With WebMethods returning reference type (i.e.
> class). I've got the following error when running it in debug mode :
>
> System.InvalidOperationException: There was an error generating the
> XML document. ---> System.StackOverflowException: Exception of type
> System.StackOverflowException was thrown.
> --- End of inner exception stack trace ---
> at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter
> xmlWriter, Object o, XmlSerializerNamespaces namespaces)
> at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter
> textWriter, Object o, XmlSerializerNamespaces namespaces)
> at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter
> textWriter, Object o)
> at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse
> response, Stream outputStream, Object returnValue)
> at
System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[]
> returnValues, Stream outputStream)
> at
System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[]
> returnValues)
> at System.Web.Services.Protocols.WebServiceHandler.Invoke()
> at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
>
> Here is a piece of my code :
>
>
> // Here come Using statements
> // ....
>
> [WebService (Description="wsBR Web Service", Name="wsBR",
> Namespace="http://localhost/wsBR/wsBR")]
> public class wsBR : System.Web.Services.WebService
> {
> public wsBR()
> {
> //CODEGEN: This call is required by the ASP.NET Web Services
> Designer
> InitializeComponent();
> }
>
> //Generated code (InitializeComponent)
> //....
>
> [WebMethod (Description="Company by Code",
> MessageName="GetCompanyByCode")]
> public Company GetCompany(string code)
> {
> return new Company(code);
> }
>
>
> [WebMethod (Description="Company by ID",
> MessageName="GetCompanyByID")]
> public Company GetCompany(long id)
> {
> return new Company(id);
> }
>
> //Other Web methods
> //....
>
> }
>
>
>
>
>
> [XmlRoot()]
> public class Company
> {
> private long lID = -1;
> private string sCode = string.Empty;
> // Other private members (all value type)
> // ....
>
> [XmlElement()]
> public long ID
> {
> get
> {
> return lID;
> }
> set
> {
> lID = value;
> }
> }
>
> [XmlElement()]
> public string Code
> {
> get
> {
> return sCode;
> }
> set
> {
> sCode = value;
> }
> }
>
> // Other properties
> // ....
>
>
> // Without default constructor get serialize error
> public Company ()
> {
> }
>
> public Company (string code)
> {
> IDataReader oReader = DataFactory.CompanyByCode (code);
> if (oReader.Read ())
> {
> // Initialize private members
> // ....
> } oReader.Close ();
> oReader.Dispose ();
> }
>
> public Company (long id)
> {
> IDataReader oReader = DataFactory.CompanyByID (id);
> if (oReader.Read ())
> {
> // Initialize private members
> // ....
> } oReader.Close ();
> oReader.Dispose ();
> }
> }
>
> I made several tests :
>
> - without Xml Attributes ([XmlElement()], [XmlRoot()])
> - without overloaded constructor (I keep only one of them removing
> corresponding web method)
> - without Xml Attributes and overlaoded constructor
>
> and still got the same error.
>
> But when I change my private members to public members (removing
> corresponding property) it's working fine !
>
> Could someone explains me why this error occurs ?
>
> Thanks in advance,
>
> Nik