0

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.

askdev
  • 1
  • 2
  • Not sure if this is the problem but you shouldnt be calling a WCF service with ByRef parameters. Your GetUpdates should return the Transaction Array and not a bool. Thats bad practice. If the method has an issue then it should throw an error back to the client and not return a false. If you need to return multiple things then create a DTO object with properties for all your return values. – Kevbo Apr 09 '18 at 17:57
  • ALSO, your 3 child classes need all the DataContract and DataMember attributes – Kevbo Apr 09 '18 at 18:03
  • Thanks for the responses Kevbo, i have tried removing the ByRef parameter and made my server side classes to have the DataContract and DataMember attributes. Unfortunately these didnt help, i am still running into the same error. – askdev Apr 10 '18 at 02:14
  • Fyi... with the ByRef parameter, the Transaction Array is returned perfectly fine when i use the Integer and string arrays, but its not working with the custom class array. – askdev Apr 10 '18 at 02:16
  • as i mentioned in the previous comment... you shouldnt use byref parameters. If you need to modify an array or some other kind of data then pass in the data via the parameter but return it from the function.... Public Function GetUpdates(ByVal RequestType As Integer, ByVal Info As Transaction()) As Transaction() – Kevbo Apr 10 '18 at 20:38
  • I would like to address your actual problem though and that is it appears like you have an issue with your endpoint declaration in the config file. Are you running this on your local PC or is this error happening on the web server? – Kevbo Apr 10 '18 at 20:53
  • Thanks for your input, i have already removed the ByRef parameter and added the DataContract and DataMember attributes, this hasnt helped and i am running into the same error. I am using this on a local pc. – askdev Apr 12 '18 at 01:36
  • I would start by stripping out your complex function and start by just returning a single entity of your classes to make sure they are serializable and able to be called by the client. Even if you just return an empy instance it will tell you if it can work. then add your classes one by one until you find the problem. Trying to create a complex function with all these classes is hard to diagnose. – Kevbo Apr 12 '18 at 15:30
  • Thanks for your input Kevbo. The issue is with WCF not being able to serialize an indexed property. i was able to catch the error after enabling the message logging and exceptions in the WCF configuration file. Thanks for the help, the issue is resolved. – askdev Apr 18 '18 at 01:52
  • thats great. how about giving me an up-tic on my suggestions? That helps us out – Kevbo Apr 18 '18 at 14:21
  • Sure Kevbo, thanks for the help. I dont see an option to uptick your suggestions, how do i do it? I thought we can only uptick an answer. – askdev Apr 18 '18 at 22:06
  • when you hover over my comments you should see a small arrow show up on the left side of the comment. Just click the up arrow – Kevbo Apr 19 '18 at 14:05

1 Answers1

0

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.