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



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

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

服务器端类结构:

'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 数据协定:

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 请求上下文(可能是由于服务关闭)。有关更多详细信息,请参阅服务器日志。

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

在 Visual Studio 的客户端项目"连接的服务"上,右键单击 WCF 名称,选择"配置引用服务",在"集合类型"上选择"System.Collections.Generic.List",单击"确定"。 再次测试。

相关内容

  • 没有找到相关文章

最新更新