title: Choosing correct connection properties for long running WCF applications
body: <p>I am writing a client/server application in C# using WCF. All my testings went fine, but as soon as I deployed the service, I noticed random problems in communicating with the server.
  I enabled debugging and saw messages like this in the server:</p>
  <pre><code>The communication object, System.ServiceModel.Channels.ServerReliableDuplexSessionChannel, cannot be used for communication because it has been Aborted.
  </code></pre>
  <p>The pattern is like this:</p>
  <ul>
  <li>client is sending query</li>
  <li>service is processing the query</li>
  <li>service is sending something back</li>
  <li>Activity boundary is of level &quot;Stop&quot; - everything seems fine</li>
  <li>Add inactivityTimeout of reliable session to the datetime of last contact and you have the timestamp of the exception thrown by the service</li>
  </ul>
  <p>The application goes like this: The service instance provides an API methods to interact with a database and is of type &quot;netTcpBinding&quot;. Several clients (about 40) are connected and randomly calling methods from the service. The clients can stay open for several days, even without sending or receiving anything.</p>
  <p>Here are the relevant bits:</p>
  <p><strong>Service</strong>:</p>
  <pre><code>    [ServiceContract(CallbackContract = typeof(ISVCCallback), SessionMode = SessionMode.Required)]
      [ExceptionMarshallingBehavior]
  ...
  </code></pre>
  <p>and</p>
  <pre><code>    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext=true)]
      public class SVCService : ISVC
  ...
  </code></pre>
  <p><strong>Service configuration</strong>:</p>
  <pre><code>    &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name=&quot;behaviorConfig&quot;&gt;
            &lt;serviceMetadata httpGetEnabled=&quot;false&quot; httpGetUrl=&quot;&quot; /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults=&quot;true&quot; /&gt;
            &lt;serviceThrottling maxConcurrentCalls=&quot;50&quot; maxConcurrentSessions=&quot;1000&quot;
              maxConcurrentInstances=&quot;50&quot; /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
  
      &lt;bindings&gt;
        &lt;netTcpBinding&gt;
          &lt;binding name=&quot;tcpBinding&quot; closeTimeout=&quot;00:01:00&quot; openTimeout=&quot;00:10:00&quot;
            receiveTimeout=&quot;23:59:59&quot; sendTimeout=&quot;00:01:30&quot; transferMode=&quot;Buffered&quot;
            listenBacklog=&quot;1000&quot; maxBufferPoolSize=&quot;671088640&quot; maxBufferSize=&quot;671088640&quot;
            maxConnections=&quot;1000&quot; maxReceivedMessageSize=&quot;671088640&quot;     portSharingEnabled=&quot;true&quot;&gt;
            &lt;readerQuotas maxStringContentLength=&quot;671088640&quot; maxArrayLength=&quot;671088640&quot;
              maxBytesPerRead=&quot;671088640&quot; /&gt;
            &lt;reliableSession inactivityTimeout=&quot;23:59:59&quot; enabled=&quot;true&quot; /&gt;
            &lt;security mode=&quot;None&quot;&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/netTcpBinding&gt;
      &lt;/bindings&gt;
  </code></pre>
  <p><strong>Client configuration</strong>:</p>
  <pre><code>        &lt;bindings&gt;
              &lt;netTcpBinding&gt;
                  &lt;binding name=&quot;NetTcpBinding_ISVC&quot; closeTimeout=&quot;00:01:00&quot; openTimeout=&quot;00:10:00&quot;
                      receiveTimeout=&quot;23:59:59&quot; sendTimeout=&quot;00:01:30&quot; transactionFlow=&quot;false&quot;
                      transferMode=&quot;Buffered&quot; transactionProtocol=&quot;OleTransactions&quot;
                      hostNameComparisonMode=&quot;StrongWildcard&quot; listenBacklog=&quot;1000&quot;
                      maxBufferPoolSize=&quot;671088640&quot; maxBufferSize=&quot;671088640&quot; maxConnections=&quot;1000&quot;
                      maxReceivedMessageSize=&quot;671088640&quot;&gt;
                      &lt;readerQuotas maxStringContentLength=&quot;671088640&quot; maxArrayLength=&quot;671088640&quot;
                          maxBytesPerRead=&quot;671088640&quot; /&gt;
                      &lt;reliableSession ordered=&quot;true&quot; inactivityTimeout=&quot;23:59:59&quot;
                          enabled=&quot;true&quot; /&gt;
                      &lt;security mode=&quot;None&quot;&gt;
                          &lt;message clientCredentialType=&quot;Windows&quot; /&gt;
                      &lt;/security&gt;
                  &lt;/binding&gt;
              &lt;/netTcpBinding&gt;
          &lt;/bindings&gt;
  </code></pre>
  <p>Is there anything wrong here? What is the best configuration for these kind of applications?</p>
  <h3>Update:</h3>
  <p>I encountered one thing:</p>
  <p>In one service contract, I change something and notify all connected clients. It usually works fine, at least in my tests. But last &quot;crash&quot; or &quot;freeze&quot; I stepped through the log and saw that the latest function was where I use callback contracts to notify the clients.</p>
  <p>What I want to do there: I save something to the database and at the end I notify all connected clients of the change. I think that the list of connected clients is not current anymore and it runs into a timeout at this step.</p>
  <p>Now the question is how to avoid these timeouts.</p>
  <ul>
  <li>Should I use threading in the service? I think the threads will be killed as soon as the service call ends, am I right here?</li>
  <li>I could implement a static queue function which does all the callback-notification (this is something Marc_S suggested)</li>
  <li>Is there a way to reliably detect connection drops inside the server?</li>
  </ul>