wcf “An existing connection was forcibly closed by

2020-07-18 04:01发布

问题:

I'm getting the error "An existing connection was forcibly closed by the remote host" after I close the Client program. I added this code to make sure to close the client connection when the program gets closed.

I have also a button to close the client and the button works without an error.

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;
    }
}

Do i miss something here? This is the stacktrace:

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

I hope the stacktrace helps someone to help me :D I'm using nettcp and the error only occurs when i close the program.

Thanks, Manuel

UPDATE: wcf config: Server:

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

Client:

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

回答1:

There are several possible causes of this error, but the most usual cause is sending data when the peer had already closed to connection: in other words, an application protocol error. It is also possible that the peer has misbehaved by deliberately resetting the connection instead of closing it normally. In either case there isn't much you can do at runtime other than close the socket and forget about the peer. But you should investigate the causes, prior to compile time.



回答2:

There are number of reason for this error. if serialized object is not well formed also this kinda error will be thrown from wcf. consider an example: in my requirement i have to send master data(say for ex : user,product,country) to client through a response class object. In that i have added generic class Object i have used rather than,main class object like product,country.At that time, i got the same error.

Public Property genericObj As Object

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

the above code caused me same error,what you are getting.

then i changed to as shown below :

  <DataMember()>
    Public Property loginObject As csCSLogin

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


标签: c# wcf tcp