[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.webservices

System.StackOverflowException when calling webmethod

(Nicolas Mayoraz)

1/21/2003 10:18:00 PM

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
3 Answers

Marina

1/21/2003 7:37:00 PM

0

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


David Carlson [MSFT]

1/21/2003 8:30:00 PM

0

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/c...
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.

"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


(Nicolas Mayoraz)

1/22/2003 2:22:00 PM

0

"David Carlson [MSFT]" <davidcar@online.microsoft.com> wrote in message news:<uiiXOOYwCHA.1676@TK2MSFTNGP10>...
> 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 ?!

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

Thanks, I'll have a look at it