I have an ASMX webservice with a number of methods which will return XML.
The service returns various different objects and I have created a wrapper object which contains information about the request e.g:
[Serializable]
[XmlRoot("response")]
public class DtoWrapper<T>
{
[XmlElement("error")]
public bool Error { get; set; }
[XmlElement("error_message")]
public string ErrorMessage { get; set; }
[XmlElement("success")]
public bool Success { get; set; }
[XmlElement("friendly_message")]
public string FriendlyMessage { get; set; }
[XmlArray("result")]
[XmlArrayItem("item")]
public List<T> Payload { get; set; }
}
Now this works fine until I defined my second method with a different type. Then I get this error when I try and load the ASMX test page
The top XML element ‘response’ from namespace ‘http://tempuri.org/’
references distinct types
MyProject.Web.webservices.DtoWrapper1[MyProject.BusinessLogic.ClassA]1[MyProject.BusinessLogic.ClassB].
and
MyProject.Web.webservices.DtoWrapper
Use XML attributes to specify another
XML name or namespace for the element
or types.
I have tried marking my objects up with [XmlType(Namespace="com.temp.A")] and [XmlType(Namespace="com.temp.B")] but it doesn’t seem to help.
Any ideas? Will I have to create a wrapper object for each type I want to use?
EDIT: I’ve realised it’s not actually the type arguments that are the problem. It’s the fact that the [XmlRoot] tag is specified on the class. The serializer is treating them as 2 types but they have the same root element in the same namespace.
You cannot do this. XML has no concept of generics, neither do XML Schema or SOAP. As far as XML Schema is concerned, if it has the same element name and same namespace, then it’s the same thing.
You cannot have a generic web service, as the concepts do not exist.