using custom collections in web services

err.number

New member
Joined
Oct 23, 2006
Messages
1
Programming Experience
5-10
Ok, i've got a web services that returns

WidgetCollection

which just inherits CollectionBase.. it's a collection of Widget objects (obviously) ..

simple .asmx with a webmethod that returns a WidgetCollection

Now, when I view the WSDL, it serializes my WidgetCollection type to a complex type

HTML:
</s:complexType>
 
<s:complexType name="ArrayOfWidget">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="Widget" nillable="true" type="tns:Widget" /> 
 
</s:sequence>
 
 
</s:complexType>

I create a simple test client .aspx page, and when I add the web reference, I need to create a local object to hold the output of my service.. it will pickup the service.Widget class in intellisense, but not the WidgetCollection type.. I try using ArrayList, but that wouldn't compile..


I hope I'm making sense here.. Thanks in advance
 
Last edited by a moderator:
If you make the service class public it will expose. Code use is typically:
VB.NET:
Dim webserv As New LocalServiceNamespace.Service
Dim x As LocalServiceNamespace.customClass = webserv.getCustomType
For the service this would be the Code:
VB.NET:
Public Class Service
    Inherits System.Web.Services.WebService
 
    Public Class customClass
        Public int As Integer
    End Class
 
    <WebMethod()> _
    Public Function getCustomType() As customClass
        Dim x As New customClass
        x.int = 5
        Return x
    End Function
End Class
It doesn't matter if customClass is defined within Service class or outside.
 
If just found that if you inherit a collection and return an instance of this indeed an array of its strong type items will be returned instead of the custom collection instance. So if a strong type collection of the above example customClass was returned you would have to catch it like: Dim x() As LocalServiceNamespace.customClass or do For Each or add the same inherited collection in the client with a common AddRange method.
 
Back
Top