title: WCF Duplex Service Channel Close
body: <p>I have an application based around a WCF Duplex service. I have problems when the user "Restarts" the work the application does... under the hood, the client side closes the connection to the WCF service and creates another. The Service contract is defined like so...</p>
  
  <pre><code>[ServiceContract(Namespace="net.tcp://namespace.MyService",
      SessionMode=SessionMode.Required,
      CallbackContract=typeof(IServiceCallback))]
  public interface IMyService
  {
      [OperationContract(IsOneWay=true)]
      void DoWork();
  }
  
  
  public interface IServiceCallback
  {
      [OperationContract(IsOneWay=true)]
      void SendMessage(string message);
  }
  </code></pre>
  
  <p>The implementation is defined as:</p>
  
  <pre><code>[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single,
      InstanceContextMode = InstanceContextMode.PerSession,
      UseSynchronizationContext = false,
      IncludeExceptionDetailInFaults = true)]
  public class MyService : IMyService
  {
      public void DoWork()
      {
          var callback = OperationContext.Current.GetCallbackChannel&lt;IServiceCallback&gt;();
          callback.SendMessage("Hello, world.");
      }
  }
  </code></pre>
  
  <p>The configuration for the client is as follows:</p>
  
  <pre><code>  &lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;netTcpBinding&gt;
          &lt;binding name="net.tcp" receiveTimeout="02:00:00" sendTimeout="02:00:00" maxReceivedMessageSize="2147483647"&gt;
            &lt;security mode="None"/&gt;
          &lt;/binding&gt;
        &lt;/netTcpBinding&gt;
      &lt;/bindings&gt;
      &lt;client&gt;
        &lt;endpoint address="net.tcp://localhost:8000/MyService/MyService"
            binding="netTcpBinding" bindingConfiguration="net.tcp" contract="ExternalServiceReference.IMyService"&gt;
        &lt;/endpoint&gt;
      &lt;/client&gt;
    &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>Config for the service:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
  &lt;behaviors&gt;
    &lt;serviceBehaviors&gt;
      &lt;behavior name="serviceBehaviour"&gt;
        &lt;serviceMetadata /&gt;
      &lt;/behavior&gt;
    &lt;/serviceBehaviors&gt;
  &lt;/behaviors&gt;
  &lt;bindings&gt;
    &lt;netTcpBinding&gt;
      &lt;binding name="netTcp" sendTimeout="01:00:00" receiveTimeout="01:00:00" &gt;
        &lt;security mode="None"&gt;
        &lt;/security&gt;
      &lt;/binding&gt;
    &lt;/netTcpBinding&gt;
  &lt;/bindings&gt;
  &lt;services&gt;
    &lt;service behaviorConfiguration="serviceBehaviour" name="MyService.MyService"&gt;
      &lt;endpoint address="MyService" binding="netTcpBinding" bindingConfiguration="netTcp" name="net.tcp" contract="MyService.IMyService" /&gt;
      &lt;endpoint binding="mexTcpBinding" bindingConfiguration="" name="net.tcp" contract="IMetadataExchange" /&gt;
      &lt;host&gt;
        &lt;baseAddresses&gt;
          &lt;add baseAddress="net.tcp://localhost:8000/MyService" /&gt;
        &lt;/baseAddresses&gt;
      &lt;/host&gt;
    &lt;/service&gt;
  &lt;/services&gt;
  </code></pre>
  
  <p></p>
  
  <p>In the client's contructor:</p>
  
  <pre><code>var callback = new CallbackImplementation();
  _context = new InstanceContext(callback);
  _proxy = new MyServiceProxy(_context);
  </code></pre>
  
  <p>I'm trying the following before I establish a new connection:</p>
  
  <pre><code>        try
          {
              if (_context != null)
              {
                  _context.ReleaseServiceInstance();
                  _context.Close();                    
              }
          }
          catch (Exception ex)
          {
              Debug.WriteLine(ex.Message);
              if (_context != null)
              {
                  _context.Abort();
              }
          }
  </code></pre>
  
  <p>The issue I see is that the _context.Close() call always times out and throws an exception. Although I'm then aborting the channel, this feels wrong to me, and I believe it's the cause of freezing in my application. Does anybody know why the Close() call fails?</p>
  
  <p><strong>EDIT:</strong> I missed something earlier regarding my callback implementation that might be relevant. It looks something like this:</p>
  
  <pre><code>[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Single, 
      UseSynchronizationContext = false, 
      IncludeExceptionDetailInFaults = true)]
  public class CallbackImplementation : IServiceCallback
  {
      public void SendMessage(string message)
      {
          // Do something with the message
      }
  }
  </code></pre>
  
  <p>The exception message is "The ServiceHost close operation timed out after 00:00:30.  This could be because a client failed to close a sessionful channel within the required time.  The time allotted to this operation may have been a portion of a longer timeout.". There's no inner exception.</p>
  
  <p>Thanks</p>