title: WCF Callback deadlock
body: <p>I ran into a problem with a WCF service that calls a callback method on the client. First the service:</p>
  
  <pre><code>[ServiceContract(
  SessionMode = SessionMode.Required,
  CallbackContract = typeof(IMarketObserver))]
  public interface IServer
  {
    ...
    [OperationContract]
    [FaultContractAttribute(typeof(WCFFaultDetail))]
    bool NotifyOnMarket(EnumMarkets marketID);
    ...
  }
  </code></pre>
  
  <p>Service implementation:</p>
  
  <pre><code>[ServiceBehavior(
  InstanceContextMode = InstanceContextMode.Single,
  ConcurrencyMode = ConcurrencyMode.Multiple,
  UseSynchronizationContext = false)]
  public sealed class Platform : IServer
  {
   ...
   public bool NotifyOnMarket(EnumMarkets marketID)
   {
      try
      {
          IMarketObserver callback = OperationContext.Current.GetCallbackChannel&lt;IMarketObserver&gt;();
          if (subscribers.Contains(callback) == false)
          {
              subscribers.Add(callback);
          }
      }
      catch
      {
          return false;
      }
      //This call may cause a call to the callback method SendMarketData()!!
      callSomeMethod();
      return exchangeProxy.IsMarketIDValid();
  }
  </code></pre>
  
  <p>The callback contract:</p>
  
  <pre><code>public interface IMarketObserver
  {
    [OperationContract(IsOneWay = true)]
    void SendMarketData(MarketData marketData);
  }
  </code></pre>
  
  <p>The client implementation of this callback is:</p>
  
  <pre><code>[CallbackBehavior(
      ConcurrencyMode = ConcurrencyMode.Multiple,
      UseSynchronizationContext = false)]
  public class MarketBase : 
      IServerCallback
  {
    protected IService serviceProxy;
    public void SendMarketData(MarketData marketData)
    {
        //Do something
    }
    private void NotifyOnMarkets()
    {
        foreach (EnumMarkets item in observedMarkets)
        {
            try
            {
                bool res = serviceProxy.NotifyOnMarket(item);
            }
            catch (Exception e)
            {
               ...
            }
        }
    }
  </code></pre>
  
  <p>The problem occurs when calling NotifyOnMarkets() with the foreach loop.</p>
  
  <p>If there is only one item in the observedMarkets list, so there is exactly one call to the service's NotifyOnMarket() method and everything is working fine.</p>
  
  <p>But if observedMarkets contains more than one item then NotifyOnMarket() will get called many times with a high frequency to the server.</p>
  
  <p>The implementation of NotifyOnMarket() on the server calls a method which in turn will call the callback method SendMarketData(I commented this fact).</p>
  
  <p>In the traces I can see that serviceProxy.NotifyOnMarket(item); does not return on a second item, a timeout occurs.</p>
  
  <p>On the server side, mutliple calls to NotifyOnMarket() are handled correctly and the method is exited. But as said above, the boolean result will not show up on the client(timeout).</p>
  
  <p>Also, one can see that on the server side the callback is invoked(it is one-way so no response will be returned), but on the client side nothing happens since the callback implementation is not called.</p>
  
  <p>My conclusion is that some kind of a deadlock occured and this maybe due to the client instance which locks itself and so the server can't call the callback method.</p>
  
  <p>Is it better to separate the instance context class from the class which is also performing service calls? If so, why?</p>
  
  <p>Thanks for advice,</p>
  
  <p>Juergen</p>