Serialization problem

L

luca

Hi,
I'm trying to write a WebService to transfer an object graph to a Client
application, serializing it by SOAP.
The problem is that my graph has circular references; my webmethod can be
something like this:

public class Node
{
public Node parent;
public Node child;

private string name = "";

public Node(string name)
{
this.name = name;
}
}


[WebMethod]
public Node getNode()
{
Node root = new Node("root");
Node a = new Node("a");

root.child = a;
a.parent = root;

return root;
}


This generates a loop detection. It sould not happen using Soap; looking at
the exception I discover that the WebServis doesn't use SOAP but XML:

System.InvalidOperationException: There was an error generating the XML
document. ---> System.InvalidOperationException: A circular reference was
detected while serializing an object of type BusinessLogicNS.Classe.
at
System.Xml.Serialization.XmlSerializationWriter.WriteStartElement(String
name, String ns, Object o, Boolean writePrefixed)
at
System.Xml.Serialization.XmlSerializationWriter.WriteStartElement(String
name, String ns, Object o)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1
_Classe(String n, String ns, Classe o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2
_TAFClasse(String n, String ns, TAFClasse o, Boolean isNullable, Boolean
needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1
_Classe(String n, String ns, Classe o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1
4_Classe(Object o)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle)
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()


Is it correct doing the following procedure to use SOAP?
1. creating a Reference to System.Runtime.Serialization.Formatters.Soap in
the WebService
2. declaring a USING in the WebService code: using
System.Runtime.Serialization.Formatters.Soap

Is it right?
I found different examples using SoapFormatter class to serialize to files:
.........
Stream stream = File.OpenWrite ("...");
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, class);


but I can't use it because in the WebServise I have not the stream (or I
have it?)

Thanks a lot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top