title: WCF Service - Client receiving CommunicationException after many requests to localhost
body: <p>I set up a WCF Service that is based on NetTcpBinding and I am using localhost. Client and Host run on same machine. I get a communication exception in the client consuming a specific method of the interface of the WCF Service in a while loop. The while loop is mandatory, because I am monitoring a value and action depends on change in value. The client is a clr console application and contains a dll with the service reference and the app.config file for the client. </p>
  
  <p>For trying to solve the problem I increased the openTimeout, closeTimeout, receiveTimeout, sendTimeout, maxBufferPoolSize and maxReceivedMessageSize in the binding configurations, but without any success.</p>
  
  <p>I also let the thread of the client sleep so that the method of the interface is less often called, but also without any success.</p>
  
  <p>To give you an idea, below you can find the while loop of the client that calls the method of the interface. I also attached the binding configurations of the client.</p>
  
  <p>While loop:</p>
  
  <pre><code>        while (ClientOfIPCService.getUGUDasDouble(ControlUGUD) == ValueOfControlUGUD)
          {
              Console::WriteLine("Request acProgState...");
              CurrentProgStat = ClientOfIPCService.getNCKVariableWithPathasDouble("/Channel/State/acProg[u1,1]");
              if (CurrentProgStat == 0 || CurrentProgStat == 3 || CurrentProgStat == 4)
              {
                  exit(0);
              }
          }
  </code></pre>
  
  <p>My question now is how to solve this problem? Is there any possibility that I empty the buffer of the requests? How can I avoid this Communication Exception?</p>
  
  <p><strong>Update 01/08/2019:</strong></p>
  
  <p>complete App.config file of Host:</p>
  
  <pre><code>  &lt;system.serviceModel&gt;
      &lt;behaviors&gt;
          &lt;serviceBehaviors&gt;
              &lt;behavior name="MexGet"&gt;
                  &lt;serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" /&gt;
                  &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
              &lt;/behavior&gt;              
          &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
  
  
  &lt;!--Binding Settings--&gt;
  &lt;bindings&gt;
    &lt;netTcpBinding&gt;
      &lt;binding name="BindingConfig1"
               closeTimeout="00:30:00"
               openTimeout="00:30:00"
               receiveTimeout="00:30:00"
               sendTimeout="00:30:00"
               maxReceivedMessageSize="20000000"
               maxBufferSize="20000000"
               maxBufferPoolSize="20000000"&gt;
        &lt;readerQuotas maxDepth="32"
                      maxStringContentLength="20000000"
                      maxArrayLength="20000000"
                      maxBytesPerRead="20000000"/&gt;
      &lt;/binding&gt;
    &lt;/netTcpBinding&gt;
  &lt;/bindings&gt;
  
  
  &lt;!--Service Binding--&gt;
  &lt;!--2. Configure end point with netTcpBinding--&gt;
  &lt;services&gt;
    &lt;service behaviorConfiguration="MexGet"
        name="PROKOS_IPC_SERVER.HMI_IPC"&gt;
      &lt;endpoint name="NetTcpBindingEndpoint" 
                address=""
                binding="netTcpBinding"
                bindingConfiguration="BindingConfig1"
                contract="PROKOS_IPC_SERVER.IHMI_IPC"&gt;
        &lt;identity&gt;
          &lt;dns value="localhost" /&gt;
        &lt;/identity&gt;
      &lt;/endpoint&gt;
  
  &lt;!--3. Configure the meta data exchange end point--&gt;
      &lt;endpoint name="MexTcpBidingEndpoint"
                address="mex"
                binding="mexTcpBinding"
                bindingConfiguration=""
                contract="IMetadataExchange"&gt;
        &lt;identity&gt;
          &lt;dns value="localhost" /&gt; 
        &lt;/identity&gt;
      &lt;/endpoint&gt;
      &lt;host&gt;
        &lt;baseAddresses&gt;
          &lt;add baseAddress="net.tcp://localhost:8124/HMI-IPC" /&gt;
        &lt;/baseAddresses&gt;
      &lt;/host&gt;
    &lt;/service&gt;
  &lt;/services&gt;
  </code></pre>
  
  <p></p>
  
  <p>complete App.config file of the Client:</p>
  
  <pre><code>    &lt;system.serviceModel&gt;
  
    &lt;!--Binding Settings--&gt;
    &lt;bindings&gt;
      &lt;netTcpBinding&gt;
        &lt;binding name="BindingConfig1" closeTimeout="00:30:00" openTimeout="00:30:00"
          receiveTimeout="00:30:00" sendTimeout="00:30:00" maxBufferPoolSize="20000000"
          maxReceivedMessageSize="20000000" /&gt;
      &lt;/netTcpBinding&gt;
    &lt;/bindings&gt;
  
      &lt;client&gt;
          &lt;endpoint address="net.tcp://localhost:8124/HMI-IPC" binding="netTcpBinding"
              bindingConfiguration="BindingConfig1" contract="IPC.IHMI_IPC"
              name="NetTcpBindingEndpoint"&gt;
              &lt;identity&gt;
                  &lt;dns value="localhost" /&gt;
              &lt;/identity&gt;
          &lt;/endpoint&gt;
      &lt;/client&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>And here I attached a screenshot of the Server stack trace:</p>
  
  <p><a href="https://i.stack.imgur.com/rUHH5.jpg" rel="nofollow noreferrer">Server stack trace</a></p>
  
  <p>The complete error message of the client is:
  "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:29:59.9549975'."</p>
  
  <p>I didn't handle the exception on the client side which gives me the following error message in my clr console application.</p>
  
  <p><a href="https://i.stack.imgur.com/GxfLk.png" rel="nofollow noreferrer">Error message of client</a></p>
  
  <p><strong>Update 02/08/2019:</strong></p>
  
  <p>The following code shows the idea of letting the thread sleep to avoid many requests:</p>
  
  <pre><code>        while (ClientOfIPCService.getUGUDasDouble(ControlUGUD) == ValueOfControlUGUD)
      {
          delay(500);
          Console::WriteLine("Request acProgState...");
          CurrentProgStat = ClientOfIPCService.getNCKVariableWithPathasDouble("/Channel/State/acProg[u1,1]");
          if (CurrentProgStat == 0 || CurrentProgStat == 3 || CurrentProgStat == 4)
          {
              exit(0);
          }
  
          // lets the current thread sleep for X miliseconds
          delay(500);
      }
  </code></pre>
  
  <p>The other idea is to integrate a method where I can cut the wire after the while loop and to open it again for further use.</p>
  
  <p><strong>Update 02/08/2019:</strong></p>
  
  <p>I enabled the WCF tracing as required and I could get the following exception details from the tracing (see screenshot and the translation below of the message):</p>
  
  <p>"The socket was aborted because an asynchronous receive from the socket was not completed within the assigned time limit of 00:02:00. The time period allocated for this task may have been part of a longer timeout."</p>
  
  <p><a href="https://i.stack.imgur.com/c7hco.png" rel="nofollow noreferrer">Service Trace of the WCF service</a></p>