System.StackOverflowException when calling webmethod

System.StackOverflowException when calling webmethod

Post by Nicolas Mayor » Thu, 23 Jan 2003 00:03:38



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

 
 
 

System.StackOverflowException when calling webmethod

Post by Marin » Thu, 23 Jan 2003 03:37:33


All serializable classes need to have a default constructor. Also, only
public members will be serialized.


Quote:> 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[]
Quote:> returnValues, Stream outputStream)
>    at

System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[]

- Show quoted text -

Quote:> 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


 
 
 

System.StackOverflowException when calling webmethod

Post by David Carlson [MSFT » Thu, 23 Jan 2003 04:30:47


I'm no expert at this, but I suspect "private" is the problem. From
"Introducing XML Serialization":

...*XML serialization serializes only the public fields and property values
of an object into an XML stream.* ... XML serialization does not convert
methods, indexers, private fields, or read-only properties (except read-only
collections). To serialize all of an object's fields and properties, both
public and private, use the BinaryFormatter instead of XML serialization.

For the full text of this topic, see
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgu...
l/cpconintroducingxmlserialization.asp

--
This posting is provided "AS IS" with no warranties, and confers no rights.
Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.


Quote:> 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[]
Quote:> returnValues, Stream outputStream)
>    at

System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[]

- Show quoted text -

Quote:> 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

 
 
 

System.StackOverflowException when calling webmethod

Post by Nicolas Mayor » Thu, 23 Jan 2003 22:13:31



Quote:> I'm no expert at this, but I suspect "private" is the problem. From
> "Introducing XML Serialization":

> ...*XML serialization serializes only the public fields and property values
> of an object into an XML stream.* ... XML serialization does not convert
> methods, indexers, private fields, or read-only properties (except read-only
> collections). To serialize all of an object's fields and properties, both
> public and private, use the BinaryFormatter instead of XML serialization.

But in the erroneous version of my code (the one with properties
instead of public members), properties were public and read/write. So
it should have work ?!

Quote:> For the full text of this topic, see
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgu...
> l/cpconintroducingxmlserialization.asp

Thanks, I'll have a look at it
 
 
 

1. System.StackOverflowException

Hello,

Under this message you can see my error message.
I have created two customwebcontrols, one of them needs the other.
So I do something like this:

private MyControl myControl = new MyControl();

Whats wrong with this? If I remove this line them I don't get the
message.
But I still need that control!

Thanks!

Exception of type System.StackOverflowException was thrown.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.StackOverflowException: Exception of type
System.StackOverflowException was thrown.

Source Error:

An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of
the exception can be identified using the exception stack trace below.

Stack Trace:

[StackOverflowException: Exception of type
System.StackOverflowException was thrown.]

2. Toolbar changes not being saved

3. System.StackOverFlowException?

4. Forwarding netbios-ns/dgm

5. An unhandled exception of type 'System.StackOverflowException' occurred in XmlTest.exe

6. Extra domains appearing in "Microsoft Windows Network" in Windows Explorer

7. Help System.StackOverflowException Error!!!

8. The Media Play

9. System.StackOverflowException

10. Member variables loss their values between WebMethods calls

11. NEED for WebMethods position (WebMethods, BPM) / Rosemont, Illinois / !!!!!

12. Exception when calling async version of webmethod from webdeployed client.