Passing XmlDocument-derived class in a WebMethod

Passing XmlDocument-derived class in a WebMethod

Post by olrt » Sat, 15 Jul 2006 06:35:36


Hello,

I have designed a WebService with the following method :

[WebMethod]
public DbAppAnswer GetDs(DbAppRequest request)


DbAppRequest and DbAppAnswer are defined as :

public class DbAppRequest : DbAppMessageBase
public class DbAppAnswer : DbAppMessageBase
public abstract class DbAppMessageBase : XmlDocument


Now up on the call to WebMethod GetDs, I have the following exception :

---------------------------------------------------8<-------------------------------------------------------------------------
System.Web.Services.Protocols.SoapException: Server was unable to read
request. ---> System.InvalidOperationException: There is an error in
XML document (1, 370). ---> System.InvalidCastException: Unable to cast
object of type 'System.Xml.XmlElement' to type
'DbAppFrameworkMessages.DbAppRequest'.\n at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read1_GetDs()\n
at
Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer.Deserialize(XmlSerializationReader
reader)\n at
System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,
String encodingStyle, XmlDeserializationEvents events)\n --- End of
inner exception stack trace ---\n at
System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,
String encodingStyle, XmlDeserializationEvents events)\n at
System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,
String encodingStyle)\n at
System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()\n
--- End of inner exception stack trace ---\n at
System.Web.Services.Protocols.SoapServerProtocol.ReadParameters()\n
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
---------------------------------------------------8<-------------------------------------------------------------------------


Another thing I noticed is that the generated proxy has this signature
:

public System.Xml.XmlNode GetDs(System.Xml.XmlNode request)

Well I understand that there's a problem on the server-side : the
request is sent as an XmlElement and
there's an exception casting an XmlElement to DbAppRequest.


What guidance could you give me ?
Should I write a "cast-operator" in DbAppRequest so as to automatically
cast XmlElement to DbAppRequest ?


Also, what best practices could you give me so as to optimize the body
of the SOAP message since DbAppRequest is already XML.

Thanks a lot for your answers,
Olivier.
 
 
 

Passing XmlDocument-derived class in a WebMethod

Post by olrt » Sun, 16 Jul 2006 05:03:46

Solved the problem by declaring :

public abstract class DbAppMessageBase : XmlDocument,IXmlSerializable


Here is the implementation of IXmlSerializable :
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}

public void ReadXml(XmlReader reader)
{
InnerXml=reader.ReadString();
}

public void WriteXml(XmlWriter writer)
{
writer.WriteString(InnerXml);
}


I had also to edit manually the WSDL-generated proxy because it
generated methods with DataSet parameters :-(.
Replacing with the proper types (DbAppAnswer and DbAppRequest) worked.

Well it seems that WSDL has trouble generating methods from WebMethods
taking parameters declared as user-classes...
Can somebody at MS confirm ??