title: WCF duplex channel gets closed when using callbacks
body: <p>My WCF service uses netTcpBinding, and has a callback object.</p>
  
  <p>I need to service multiple concurrent clients, and mantain sessions, so the service is decorated with</p>
  
  <pre><code>[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple]
  </code></pre>
  
  <p>To avoid thread deadlocks, the callback class is decorated with</p>
  
  <pre><code>[CallbackBehavior(UseSynchronizationContext=false)]
  </code></pre>
  
  <p>and I use <code>SynchronizationContext</code> to execute the method in the UI thread.</p>
  
  <p>The problem is that sometimes the channel gest closed with no reason (<code>ICommunicationObject.Closing</code> event gets fired). After that, I get exceptions in any subsequent service call.</p>
  
  <p>Looking in the trace file, the last message is a callback call, however, the callback method never gets invoked. There are no exceptions.</p>
  
  <p>After some debugging I identified that this happens only when the callback call is made in the middle of a synchronous operation. The steps would be this:</p>
  
  <ol>
  <li>Call to service method <code>A</code> with <code>IsOneWay=true</code></li>
  <li>Call to service method <code>B</code> with <code>IsOneWay=false</code></li>
  <li><code>A</code> invokes a callback method, but <code>B</code> is still executing.</li>
  </ol>
  
  <p>This shouldn't be a problem because the callback has <code>UseSynchronizationContext=false</code>, so the callback call could be attended in a separate thread.</p>
  
  <p>I was unable to reproduce the problem in a simpler scenario. Following these steps in a simple project executes successfully.</p>
  
  <p>Any idea of what could be happening or how to identify the problem?</p>