Learner
Learner

Reputation: 1542

An existing connection was forcibly closed by the remote host

My service's app.config

<system.serviceModel>
<client>
  <endpoint name="DataLocal" address="net.tcp://SomeAddress" binding="netTcpBinding" contract="ISomeContract" bindingConfiguration="TcpCustomSecurity" behaviorConfiguration="SecureBehaviorName">
    <identity>
      <dns value="localhost"/>
    </identity>
  </endpoint>
</client>
  <services>     
  <service name="SomeService">
    <host>
      <baseAddresses>
        <add baseAddress="http://SomeService" />
        <add baseAddress="net.tcp://SomeService" />
      </baseAddresses>
    </host>
    <endpoint name="SomeService_Normal" address="Secure" binding="netTcpBinding" contract="ISomeService" bindingConfiguration="TcpNormal"/>
  </service>
</services>
<bindings>
  <netTcpBinding>        
    <binding name="TcpNormal" transferMode="Buffered" receiveTimeout="24.20:31:23.6470000">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
      <security mode="None" />
    </binding>
    <binding name="TcpCustomSecurity" transferMode="Buffered" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="24.20:31:23.6470000" sendTimeout="00:01:00" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
      <security mode="Message">
        <message clientCredentialType="UserName"/>
      </security>
    </binding>
  </netTcpBinding>   
</bindings>
<behaviors>
  <endpointBehaviors>
    <behavior name="SecureBehaviorName">
      <clientCredentials>
        <serviceCertificate>
          <authentication certificateValidationMode="None"/>
        </serviceCertificate>
      </clientCredentials>
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </endpointBehaviors>
</behaviors>

Following are the stack statements for the error i am getting..

>          System.dll!System.Net.Sockets.Socket.Send(byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags) + 0x5a bytes 
        System.ServiceModel.dll!System.ServiceModel.Channels.SocketConnection.Write(byte[] buffer, int offset, int size, bool immediate, System.TimeSpan timeout) + 0xa7 bytes   
        System.ServiceModel.dll!System.ServiceModel.Channels.SocketConnection.Write(byte[] buffer, int offset, int size, bool immediate, System.TimeSpan timeout, System.ServiceModel.Channels.BufferManager bufferManager) + 0x34 bytes   
        System.ServiceModel.dll!System.ServiceModel.Channels.BufferedConnection.WriteNow(byte[] buffer, int offset, int size, System.TimeSpan timeout, System.ServiceModel.Channels.BufferManager bufferManager) + 0x90 bytes    
        System.ServiceModel.dll!System.ServiceModel.Channels.BufferedConnection.Write(byte[] buffer, int offset, int size, bool immediate, System.TimeSpan timeout, System.ServiceModel.Channels.BufferManager bufferManager) + 0x47 bytes 
         System.ServiceModel.dll!System.ServiceModel.Channels.FramingDuplexSessionChannel.OnSend(System.ServiceModel.Channels.Message message, System.TimeSpan timeout) + 0x115 bytes  
         System.ServiceModel.dll!System.ServiceModel.Channels.OutputChannel.Send(System.ServiceModel.Channels.Message message, System.TimeSpan timeout) + 0x81 bytes    
         System.ServiceModel.dll!System.ServiceModel.Dispatcher.DuplexChannelBinder.Request(System.ServiceModel.Channels.Message message, System.TimeSpan timeout) + 0x154 bytes  
        System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.Call(string action, bool oneway, System.ServiceModel.Dispatcher.ProxyOperationRuntime operation, object[] ins, object[] outs, System.TimeSpan timeout) + 0x206 bytes           
         System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(System.Runtime.Remoting.Messaging.IMethodCallMessage methodCall, System.ServiceModel.Dispatcher.ProxyOperationRuntime operation) + 0x59 bytes   
         System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannelProxy.Invoke(System.Runtime.Remoting.Messaging.IMessage message) + 0x65 bytes            
        mscorlib.dll!System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(ref System.Runtime.Remoting.Proxies.MessageData msgData, int type) + 0xee bytes  

// First the service call in the above stack...

Service Behavior is this: [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

The error occured after few hours of constant testing...unable to reproduce it while debugging..

EDIT:

I've got the tracing results thanks to Dejan...

on the first occurance of error i get this message: Maximum number of pending connections has been reached.

on the second occurance i get this message : The system hit the limit set for throttle 'MaxConcurrentConnections'.Limit for this throttle was set to 200.Throttle value can be changed by modifying attribute 'maxConcurrentSessions' in service throttle element.

So for the second message i did this:

and assigned this service behavior to the service. do i need to set the maxConnections on the binding for the first message.Moreover, which is the root cause for the problem , first one or second one or both. Because in the subsequent messages i always get the second message.

Please guide to update my app.config to avoid such scenario.

UPDATE

I've got the tracing results...

on the first occurance i get this message: Maximum number of pending connections has been reached.

on the second occurance i get this message : The system hit the limit set for throttle 'MaxConcurrentConnections'.Limit for this throttle was set to 200.Throttle value can be changed by modifying attribute 'maxConcurrentSessions' in service throttle element.

So for the second message i did this:

 <serviceBehaviors>
    <behavior name="tcpNormalBehavior">
      <serviceThrottling maxConcurrentSessions="800" maxConcurrentInstances="800" maxConcurrentCalls="200" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
    </behavior>
  </serviceBehaviors>

and assigned this service behavior to the service. do i need to set the maxConnections on the binding for the first message.Moreover, which is the root cause for the problem , first one or second one or both. Because in the subsequent messages i always get the second message.

Please guide.

Upvotes: 2

Views: 6378

Answers (1)

Dejan Janjušević
Dejan Janjušević

Reputation: 3230

There is a possibilty that you have an unhandled exception such as stack overflow somewhere in your WCF service. If I get it correctly, the service works but stops working after a few hours.

The first step to debugging a WCF service is WCF Tracing. Enable it, try to test your service and after it stops working again, there should be a trace entry in the .svclog file.

EDIT
Regarding your updated question: You get that exception because you are not closing the connection from the client side after you're done with the call. Try refactoring your code so calls to WCF look like this:

WCFProxy clientProxy = null;
try
{
    clientProxy = new WCFProxy();
    clientProxy.SomeCall();
    clientProxy.Close();
}
catch (Exception)
{
    if (clientProxy != null)
    {
        clientProxy.Abort();
    }
    throw;
}

and then see if it will cause problems.

Upvotes: 1

Related Questions