I have a webservice that always returns a ReturnObject that is a class I have written like this:
public class ReturnObject
{
public Status Status { get; set; }
public Object Data { get; set; }
}
When I return a single object like an int or string etc in Data it is OK but when I want to return a list of typed objects the client that running the webservice as a WebReference getting this exception:
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document ---> System.InvalidOperationException: Typen System.Collections.Generic.List`1[[Transticket.Domain.Models.ApsisMailingList, Transticket.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] kan inte användas i den här kontexten. vid System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType) vid Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType) vid Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_ReturnObject(String n, String ns, ReturnObject o, Boolean isNullable, Boolean needType)
vid Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_GetListsResponse(Object[] p) vid Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer5.Serialize(Object objectToSerialize, XmlSerializationWriter writer) vid System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) --- Slut på stackspårning för interna undantag --- vid System.Xml.Serialization.XmlSerializer.Serialize(Xml开发者_开发技巧Writer xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) vid System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream) vid System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues) vid System.Web.Services.Protocols.WebServiceHandler.Invoke() --- Slut på stackspårning för interna undantag ---
This exception is not in the webservice, it's in the client call to the webservice after returning the data.
If I do another ReturnObject there and I specify a list of typed objects as data, it works well.
Someone have an idea how I generalize this? I have always thought that List is a type of Object but looks like a .NET bug, or I have missed something?
XmlSerializer
does not include type information in the output (contrast with BinaryFormatter
); as such, there is no robust way of deserializing object
, as the xml could be (quite literally) anything.
The same is true of most contract-based serializers (DataContractSerializer
, protobuf-net, etc).
Basically: don't do that. object
is not a useful thing to use on a web-service, and that cannot work.
精彩评论