hi there,
i have a webservice which my function calls a database( eg. customer table),
what i have is my own Customer class, and i want to return an array of my
"Customer" objects.
here is my code i use (c.GetList returns an arraylist of customer objects
from the database:
<WebMethod()> _
Function GetClientList() As ArrayList
Dim c As New getAway.Client
Return c.GetList()
End Function
when i run it, it returns the following error:
System.InvalidOperationException: There was an error generating the XML
document. ---> System.InvalidOperationException: The type getAway.Client was
not expected. Use the XmlInclude or SoapInclude attribute to specify types
that are not known statically.
at
System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String
name, String ns, Object o, Boolean xsiType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1
_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5
_ArrayOfAnyType(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()
any help appreciated!
thanks,
Paul.
Elena Kharitidi - 03 Sep 2004 02:19 GMT
When an un-typed array is returned from a web method, customer has to
specify (via custom serialization attributes) objects of what type can be
expected as items, this can be done by adding custom serialization
attributes to the return type:
(1)
Imports System.Xml.Serialization
<WebMethod()> _
Function GetClientList() As <XmlElement(GetType(getAway.Client))> ArrayList
Dim c As New getAway.Client
Return c.GetList()
End Function
Or by adding XmlInclude attribute to the service class:
(2)
Imports System.Xml.Serialization
<XmlInclude(GetType(getAway.Client))> _
Public Class ADSearcher
End Class
Please note that you need to do only on of the two, and depending on which
one you choose, the schema (and wire signature) of your method will be
different.
Personally I like the first approach better: it provides better
strongly-typed programming model on the client.
Thanks,
Elena
Milsnips - 03 Sep 2004 11:29 GMT
thanks for your help.
I added the first example, but it still returns an error.
when i run my "asmx" page and select the GetClientList webservice, the soap
response looks fine, as it displays the client object, but the HTTP post
response example shows 'AnyType"
it still returns the same error as mentioned in the first post.
any ideas?
thanks
Paul.
> When an un-typed array is returned from a web method, customer has to
> specify (via custom serialization attributes) objects of what type can be
[quoted text clipped - 27 lines]
> Thanks,
> Elena
Elena Kharitidi - 23 Sep 2004 18:00 GMT
Sorry about the misinformation:
There is known bug in .Net Framework preventing using XmlElement attribute
on the return array.
I am afraid you have to use XmlInclude attribute instead.
Also note that is you intend return more then one type of object, all of
them have to be listed:
<System.Web.Services.WebServiceAttribute([Namespace]:="http://tempuri.org/")
, _
System.Web.Services.WebServiceBindingAttribute(Name:="TestSoap",
[Namespace]:="http://tempuri.org/"), _
System.Xml.Serialization.XmlIncludeAttribute(GetType(MyClass1)), _
System.Xml.Serialization.XmlIncludeAttribute(GetType(MyClass2)), _
System.Xml.Serialization.XmlIncludeAttribute(GetType(Object()))> _
Public Class MyWebService
<System.Web.Services.WebMethodAttribute()> _
Public Function GetClientList () As ArrayList
End Function
End Class
Thanks,
Elena