Returning a generic list from a webmethod

Mar 19, 2009 at 12:11 PM
First, thank you for your time.

I am having difficulty coding the caller to a webservice that returns a generic list.  The called method returns a generic list of type OmsFileVersionInfo and the caller of this method is expecting a generic list of type OmsFileVersionInfo. Even though that seems okay to me, the IDE gives me a syntax error saying that it can't do the conversion.  The code is below with the error in comments in the body of the caller.

I have also tried to return an ArrayList instead of a generic list but that didn't pan out either.  Which leads me to these related questions:

Are there any restrictions on the types that webmethods can return?  From the IWebMisc class, I see that some of those methods are returning a generic list.  Can a webmethod return an ArrayList?  And if it can, would it be wrong to code it that way?  From the example code I see that ArrayLists are being converted to generic lists and then that generic list is returned.  In the example below I am using that technique as well.

I realize I am asking a lot of questions which might take up a lot of your time.  If time does not permit you answering all these questions, I would be satisfied with just answering the first question:  What is the syntax for the caller of the webservice that returns a generic list?

Thank you very much for your time,
Michael Carr

'============================================================
' This code is in my Windows Application.  
' This code calls a webservice method.
'============================================================
Public Sub GetFileVersions(ByVal sInstalledVersion As String)
     Dim oList As List(Of IWebService.OmsFileVersionInfo)

     ' The follwoing line of code gives the error:
     ' Value of type '1-dimensional of OmsWebServices.IWebService.OmsFileVersionInfo cannot be converted to
     ' 'System.Collections.Generic.List(Of OmsWebServices.IwebService.OmsFileVersionInfo)'
     oList = moWebServiceInterface.OmsGetFileVersionList(sInstalledVersion)    

     ' code that iterates through the list goes here
End Sub

'============================================================
' This code resides in my DNN IWeb Partial Class.
'============================================================
Namespace DotNetNuke.Modules.IWeb

Partial Public Class WebService

    <WebMethod(Description:=" OmsGetFileVersionList *OMS* |OmsWebServices| #Account# !Portal! "), SoapHeader("IWebCredentials")> _
    Public Function OmsGetFileVersionList(ByVal sInstalledVersion As String) As List(Of OmsFileVersionInfo)
         Dim oFileVersionList As ArrayList
         Dim oFileVersionController As New OmsFileVersionController

         oFileVersionList = oFileVersionController.GetFilesByVersion(sInstalledVersion)

         Return ConvertToGenericListOfFileVersionInfo(oFileVersionList)
    End Function

    Private Function ConvertToGenericListOfFileVersionInfo(ByVal aFileVersionArray As ArrayList) As List(Of OmsFileVersionInfo)
        Dim aList As New List(Of OmsFileVersionInfo)

        Try
            For Each oFVInfo As OmsFileVersionInfo In aFileVersionArray
                Dim oInfo As New OmsFileVersionInfo
                With oInfo
                    .FileVersionId = oFVInfo.FileVersionId
                    .MajorVersion = oFVInfo.MajorVersion
                    .MinorVersion = oFVInfo.MinorVersion
                    .BuildVersion = oFVInfo.BuildVersion
                    .Filename = oFVInfo.Filename
                    .Hash = oFVInfo.Hash
                    .Size = oFVInfo.Size
                    .ModifiedDate = oFVInfo.ModifiedDate

                    aList.Add(oInfo)
                End With
            Next

        Catch castEx As InvalidCastException
            Return Nothing
        Catch ex As Exception
            Return Nothing

        End Try
        Return aList
    End Function


End Class
End Namespace

Coordinator
Mar 19, 2009 at 1:39 PM
Yes you can use Arrays.

As to Generic lists:

This code:
http://iweb.adefwebserver.com/ExampleCode/HowtoextendIWebforyourownmodules/tabid/59/Default.aspx

shows that it is possible to use a generic list, perhaps you can start by altering that example code after you have it working?

Also, make sure the class OmsFileVersionInfo is declared "Public".
Also, when you create the web proxy in your caller you can indicate in the Properties page that a Generic list will be returned