title: WCF Timer based service not calling client back
body: <p>I want to make a WCF timer service where clients can register in order to get called back from the service after a certain time has passed. The problem is that the client doesn't get called back. No Exception is thrown.</p>
  
  <p>The callback interface is:</p>
  
  <pre><code>[ServiceContract]
  public interface ITimerCallbackTarget
  {
    [OperationContract(IsOneWay = true)]
    void OnTimeElapsed(int someInfo);
  }
  </code></pre>
  
  <p>The service looks like:</p>
  
  <pre><code>[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
    ConcurrencyMode = ConcurrencyMode.Single)]  
  public class TimerService : ITimerService
     private readonly Timer _timer = new Timer(2000); //System.Timers.Timer
  
     public void Subscribe()
     {
       ITimerCallbackTarget listener = 
         OperationContext.Current.GetCallbackChannel&lt;ITimerCallbackTarget&gt;();
  
       _timer.Elapsed += (p1, p2) =&gt;
       {
         listener.OnTimeElapsed(999);
        };
       _timer.Start();
     }
  </code></pre>
  
  <p>The callback method used by the client is:</p>
  
  <pre><code>private class TimerCallbackTarget : ITimerCallbackTarget
  {
    public void OnTimeElapsed(int someInfo)
    {
      Console.WriteLine(someInfo);
    }
  }
  </code></pre>
  
  <p>The client registers like this:</p>
  
  <pre><code>private static void TestTimerService()
  {
    InstanceContext callbackInstance = new InstanceContext(new TimerCallbackTarget());
  
    using (DuplexChannelFactory&lt;ITimerService&gt; dcf =
      new DuplexChannelFactory&lt;ITimerService&gt;(callbackInstance,  
        "TimerService_SecureTcpEndpoint"))
    {
      ITimerService timerProxy = dcf.CreateChannel();
  
      timerProxy.Subscribe();        
    }
  }
  </code></pre>
  
  <p>If I use a different thread at the subscribe method without <strong>Timer</strong> it works:</p>
  
  <pre><code>  ThreadPool.QueueUserWorkItem(p =&gt;
    {
      listener.OnTimeElapsed(999);
    });
  </code></pre>
  
  <p>It even works with the <strong>Timer</strong> (for three seconds) if I put a <strong>Thread.Sleep(3000)</strong> at the end of the subscribe method so my guess is that maybe the channel to the callback-object gets closed after the subscribe method is finished. Using a class-scope variable for the callback object retrieved with <strong>OperationContext.Current.GetCallbackChannel();</strong> instead of the method-scope variable didn't help.</p>
  
  <p>Previously i tried creating new Threads in the elapsed event handler of the <strong>Timer</strong> of the timer service to make it faster. An <strong>ObjectDisposedException</strong> was thrown with the message: "Cannot access a disposed object. Object name: 'System.ServiceModel.Channels.ServiceChannel". I then tried to simplify my service and found that even using only the <strong>Timer</strong> causes problems as described but I guess the exception indicates that somewhere the connection to the client's callback object is lost. It's strange that there is no excepiton if I don't make new threads in the <strong>Timer</strong> thread. The callback method just isn't called.</p>