wcf“关闭客户端后,远程主机强行关闭现有连接”

时间:2012-12-07 10:20:44

标签: c# wcf tcp

关闭客户端程序后,我收到错误“远程主机强行关闭现有连接”。 我添加了这段代码,以确保在程序关闭时关闭客户端连接。

我还有一个关闭客户端的按钮,按钮可以正常工作。

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{          
    try
    {
       client.Close();  
    }
    catch (CommunicationException ex)
    {
       client.Abort();
    }
    catch (TimeoutException ex)
    {
       client.Abort();
    }
    catch (Exception ex)
    {
       client.Abort();
       throw ex;
    }
}

我在这里想念一下吗? 这是堆栈跟踪:

< StackTrace>
bei System.ServiceModel.Channels.SocketConnection.HandleReceiveAsyncCompleted()
bei System.ServiceModel.Channels.SocketConnection.OnReceiveAsync(Object sender, SocketAsyncEventArgs eventArgs)
bei System.ServiceModel.Channels.SocketConnection.OnReceiveAsyncCompleted(Object sender, SocketAsyncEventArgs e)
bei System.Net.Sockets.SocketAsyncEventArgs.OnCompleted(SocketAsyncEventArgs e)
bei System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncFailure(SocketError socketError, Int32 bytesTransferred, SocketFlags flags)
bei System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
bei System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
< /StackTrace>

我希望stacktrace帮助别人帮助我:D 我正在使用nettcp,只有当我关闭程序时才会出现错误。

谢谢, 曼努埃尔

更新: wcf配置: 服务器:

 <serviceBehaviors>
        <behavior name="MyBehaviour">
          <serviceMetadata/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
 <services>
      <service name="Service" behaviorConfiguration="MyBehaviour">
        <host>
        </host>

        <endpoint address="" binding="netTcpBinding" bindingConfiguration="TCP" contract="IService"/>
        <endpoint address="mextcp" binding="mexTcpBinding" contract="IMetadataExchange"/>

      </service>
    </services>

<netTcpBinding>
        <binding name="TCP" portSharingEnabled="true" receiveTimeout="01:30:00" sendTimeout="01:10:00"
          openTimeout="00:10:00" closeTimeout="00:10:00">
         <security mode="None"/>
        </binding>
      </netTcpBinding>

客户端:

<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
               <binding name="NetTcpBinding_IService" receiveTimeout="01:30:00" sendTimeout="01:00:00" openTimeout="00:10:00" closeTimeout="00:10:00">
               <security mode="None" />
              </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://host/Service.svc"
                binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService"
                contract="ServiceRef.IService" name="NetTcpBinding_IService" />
        </client>
    </system.serviceModel>
</configuration>

2 个答案:

答案 0 :(得分:0)

此错误有几种可能的原因,但最常见的原因是当对等方已关闭连接时发送数据:换句话说,应用程序协议错误。通过故意重置连接而不是正常关闭连接,对等体也可能行为不端。在任何一种情况下,除了关闭套接字而忘记对等体之外,在运行时没有太多可以做的事情。但是你应该在编译之前调查原因。

答案 1 :(得分:0)

此错误有多种原因。如果序列化对象格式不正确,那么这种错误将从wcf抛出。 考虑一个例子:在我的要求中,我必须通过响应类对象将主数据(例如ex:user,product,country)发送给客户端。在那里我添加了通用类对象我已经使用而不是主类对象,如产品,country.At那时,我得到了同样的错误。

        公共属性genericObj As Object

    Public Sub New()
        MyBase.New()
        Me.genericObj = New Object
    End Sub

上面的代码给我带来了同样的错误,你得到了什么。

然后我改为如下所示:

  <DataMember()>
    Public Property loginObject As csCSLogin

    Public Sub New()
        MyBase.New()
        Me.loginObject = New csCSLogin
    End Sub