处理Array函数时的CommunicationException

时间:2014-04-11 15:39:22

标签: vb.net wcf

我正在测试单个桌面上的服务器和客户端应用程序之间的交互,并且在处理数组时遇到问题。我的界面下一步

<ServiceContract>
Public Interface IMyService
    <OperationContract>
    Function GetData() As Array   
End Interface

服务器端实现,是下一个

Public Function GetData() As Array Implements IMyService.GetData
    Return {1, 2, 3, 4, 5, 6, 7, 8}
End Function

客户端代码,是下一个

Shared Function GetData() As Array
    Dim channel As IMyService = Nothing
    Try
        channel = ChannelFactory(Of IMyService).CreateChannel(New NetTcpBinding, address)
        Return channel.GetData()
    Finally
        If channel IsNot Nothing Then
            CType(channel, IClientChannel).Close()
        End If
    End Try
End Function

我在执行返回channel.GetData()时遇到下一个异常,并且我没有遇到其他返回简单类型(double,integer等)的接口函数的问题。我的服务器应用是self hosting WCF service

System.ServiceModel.CommunicationException occurred
  HResult=-2146233087
  Message=The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:59.9270000'.
  Source=mscorlib
  StackTrace:

        at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)

        InnerException: System.Net.Sockets.SocketException
             ErrorCode=10054
             HResult=-2147467259
             Message=An existing connection was forcibly closed by the remote host
             NativeErrorCode=10054
             Source=System
             StackTrace:
                  at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
                  at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing)
             InnerException: 

1 个答案:

答案 0 :(得分:1)

尝试使用List(Of Integer)类型而不是数组。它似乎是无类型的(不幸的是,我不是vb dev。)

或者设置ServiceKnownType属性:

<ServiceContract>
Public Interface IMyService

    <OperationContract>
    <ServiceKnownType(GetType(Integer()))>
    Function GetData() As Array
End Interface

顺便简化调查WCF问题的过程,添加错误处理很有用:

Error handling

对于您的情况它是例外:键入&#39; System.Int32 []&#39;数据合同名称&#39; ArrayOfint:http://schemas.microsoft.com/2003/10/Serialization/Arrays&#39;不是预期的。考虑使用DataContractResolver或将任何静态未知的类型添加到已知类型列表中 - 例如,使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer的已知类型列表中。