VB.Net WCF Pass an array of class as a parameter to Client side

681 Views Asked by At

I have a WCF single channel implementation on the Server side and Client side. I have a custom class on the Server side that i am trying to pass to the client side. I am trying to pass an array (collection) of custom class to the client side.

Following is an example of the code implementation i have.

ServerSide class structure:

'Module name ServerModule.dll

Public Class ServerChildClass
    Public m_Integer As Integer

    Public Property mInteger As Integer
        Get
            Return m_Integer
        End Get
        Set(value As Integer)
            m_Integer = value
        End Set
    End Property
End Class

Public Class ServerChildClass2
    Public m_Integer As Integer

    Public Property mInteger As Integer
        Get
            Return m_Integer
        End Get
        Set(value As Integer)
            m_Integer = value
        End Set
    End Property
End Class

Public Class ServerChildClass3
    Public m_Integer As Integer

    Public Property mInteger As Integer
        Get
            Return m_Integer
        End Get
        Set(value As Integer)
            m_Integer = value
        End Set
    End Property
End Class

WCF DataContract:

Imports System.ServiceModel
Imports System.Runtime.Serialization
Imports ServerModule

<DataContract(Name:="Transaction")> <Serializable()>
<KnownType(GetType(List(Of Object)))>
<KnownType(GetType(Integer))>
<KnownType(GetType(ServerChildClass))>
<KnownType(GetType(ServerChildClass2))>
<KnownType(GetType(ServerChildClass3))>
Public Class Transaction
    Implements ICloneable
    Implements IExtensibleDataObject

    Public Sub New()
    End Sub

    'Add <DataMember()> here
    Public Function Clone() As Object Implements System.ICloneable.Clone 'client uses this to define query
        Dim newObject As New IHermesEngineServiceDataContract
        Return newObject
    End Function

    Public Property ExtensionData As ExtensionDataObject Implements IExtensibleDataObject.ExtensionData

    <DataMember()>
    Public Property Command As Integer

    <DataMember()>
    Public Property Client As Integer

'A list of Integer, or a list of  ServerChildClass or ServerChildClass2 or ServerChildClass3. Basically a list of objects
    <DataMember()>
    Public Property Parameters As System.Object()
End Class

WCF contract and class definition:

    'Service contract
<ServiceContract()> _
Public Interface IControlContract
    <OperationContract()>
    Function GetUpdates(ByVal RequestType As Integer, ByRef Info As Transaction()) As Boolean
End Interface

'Service contract class implementation
Public Class ControlClass
    Implements IControlContract
    Function GetUpdates(ByVal RequestType As Integer, ByRef Info As Transaction()) As Boolean Implements IControlContract.GetUpdates
        Return MyBase.Channel.GetUpdates(RequestType, Info)
    End Function
End Class

WCF Server Side Implementation:

Imports ServerModule
Public Class WCFClass
    Implements IControlContract
    Public Function GetUpdates(ByVal RequestType As Integer, ByRef Info As Transaction()) As Boolean Implements IControlContract.GetUpdates
        ReDim Info(2)
        'Array of ServerChildClass of length x
        Dim ClassArray As ArrayList
        'Array of ServerChildClass2 of length x
        Dim Class2Array As ArrayList

        If (RequestType = 1) Then

            Info(0) = New Transaction
            Info(0).Command = RequestType
            Info(0).Client = RequestType
            Info(0).Parameters = ClassArray.ToArray()

            Info(1) = New Transaction
            Info(1).Command = RequestType
            Info(1).Client = RequestType
            Info(1).Parameters = Class2Array.ToArray()

            Info(2) = New Transaction
            Info(2).Command = RequestType
            Info(2).Client = RequestType
            Info(2).Parameters = "Test String"
            End
            Return True
    End Function
End Class

When i send the following structure from the Server side to the client side, i receive the following error. "An error occurred while receiving the HTTP response to http://localhost/xyz. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details."

Please let me know where the issue is happening. How do i successfully send an array of class references as a parameter to the client side? Any help would be appreciated.

1

There are 1 best solutions below

0
user11167448 On

In Visual Studio, on your client project, Connected Services, right click on your WCF name, select "Configure Reference Service", on "Collection Type" select "System.Collections.Generic.List", click OK. Test again.