我在服务器端和客户端有一个 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 请求上下文(可能由于服务关闭)。有关更多详细信息,请参阅服务器日志。”
请让我知道问题发生在哪里。我如何成功地将一组类引用作为参数发送到客户端?任何帮助,将不胜感激。