need help returning webservice data in arraylist...

M

Milsnips

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.
 
E

Elena Kharitidi

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
 
M

Milsnips

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.
 
E

Elena Kharitidi

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
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top