VB.Net WCF将一个类数组作为参数传递给客户端

时间:2018-04-09 17:01:24

标签: vb.net wcf datacontractserializer netdatacontractserializer

我在服务器端和客户端有一个WCF单通道实现。我在服务器端有一个自定义类,我试图传递给客户端。我试图将自定义类的数组(集合)传递给客户端。

以下是我拥有的代码实现示例。

ServerSide类结构:

'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合同和类定义:

    '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服务器端实施:

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

当我从服务器端向客户端发送以下结构时,我收到以下错误。 “收到对http://localhost/xyz的HTTP响应时发生错误。这可能是由于服务端点绑定不使用HTTP协议。这也可能是由于服务器中止了HTTP请求上下文(可能是由于服务关闭)。有关详细信息,请参阅服务器日志。“

请让我知道问题出在哪里。如何成功将类引用数组作为参数发送到客户端?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

在Visual Studio中,在客户端项目Connected Services上,右键单击WCF名称,选择“配置参考服务”,在“集合类型”上选择“ System.Collections.Generic.List”,单击“确定”。再次测试。