c# - wcf "An existing connection was forcibly closed by the remote host"关闭客户端后

标签 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>

我希望堆栈跟踪可以帮助别人帮助我 :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>

最佳答案

此错误有多种可能的原因,但最常见的原因是在对等方已关闭连接时发送数据:换句话说,应用程序协议(protocol)错误。也有可能对等方行为不当,故意重置连接而不是正常关闭连接。在任何一种情况下,除了关闭套接字并忘记对等点之外,在运行时您无能为力。但是您应该在编译之前调查原因。

关于c# - wcf "An existing connection was forcibly closed by the remote host"关闭客户端后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13761108/

相关文章:

c# - 向 DataTable 中插入一个新行

c# - 此版本的应用程序不支持 Azure .ccproj

wcf - 带有对象列表的数据绑定(bind) Silverlight 组合框 - 工作但丑陋

c# - WCF 自定义行为的依赖注入(inject)

c - 在socket中,如果我们在socket()中将SOCK_STREAM作为第二个参数,将UDP作为第三个参数,会发生什么?

http - 为什么用户代理在服务器重置 TCP 后重新提交请求?

java - 在 JDBC 到 mysql 连接中指定 TCP 协议(protocol)

c# - 将 XmlSerializer 与根元素中的数组一起使用

c# - 调用简单服务失败并显示明显错误的错误消息

c# - C# 中用于文本框控件的 selectall() 属性