title: WCF ObjectDisposedException Safe Handle has been released / ASP.NET MVC3 Client / Threading
body: <p>Assume that:</p>
  
  <ul>
  <li>An ASP.NET MVC3 Page (using visual studio debug server) calls a WCF Service (self hosted, net.tcp binding). </li>
  <li>Service uses ThreadPool to do some work, but returns immediately a value to the client</li>
  <li>Client gets the value and redirects to another page.</li>
  <li>A few seconds later the Server crashes with an "ObjectDisposedException / Safe handle has been released)</li>
  </ul>
  
  <p>First I thought that the proxy would be disposed after the page redirect and the wcf service would get disposed too, but even when I'm not using the ThreadPool (by calling the method within the wcf service thread) I get the exception too. I don't get it. </p>
  
  <p>I'm not really into web programming. What am I doing wrong?</p>
  
  <p><b>Client</b><br/>
  Global.asax (registering channel factory)</p>
  
  <pre><code>protected void Application_Start()
  {
    builder.Register(c =&gt; new ChannelFactory&lt;IWcfBakeryService &gt;("")).SingleInstance();
  
    builder.Register(c =&gt; c.Resolve&lt;ChannelFactory&lt;IWcfBakeryService&gt;&gt;()
          .CreateChannel())
          .UseWcfSafeRelease();
  }
  </code></pre>
  
  <p><br>
  Repository (channel injected by Autofac)<br></p>
  
  <pre><code>public BakeryRepository(IWcfBakeryService orderService)
  {
  
  }
  
  
  public ReciepeResponse RequestReciepe(CakeDto webOrder)
  {
      [...] 
      return service.RequestReciepe(request);
  }
  </code></pre>
  
  <p><br><br>
  <b>Server</b>:<br>
  Contract</p>
  
  <pre><code>[ProtoBehavior, OperationContract(IsTerminating=false)]
  ReciepeResponse RequestReciepe(ReciepeRequest request);
  </code></pre>
  
  <p><br></p>
  
  <p>Service-Implementation<br></p>
  
  <pre><code>[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
  </code></pre>
  
  <p><br></p>
  
  <p>RequestReciepe / Service Method</p>
  
  <pre><code>ThreadPool.QueueUserWorkItem(new WaitCallback(DoWork));
  return new ReciepeResponse() { SomeValue = "Some Value"};
  </code></pre>
  
  <p><br>
  Exception Stacktrace (catched by AppDomain UnhandledException)</p>
  
  <pre><code>bei System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean&amp; success)
  bei System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean&amp; success)
  bei Microsoft.Win32.Win32Native.SetEvent(SafeWaitHandle handle)
  bei System.Threading.EventWaitHandle.Set()
  bei System.Runtime.Remoting.Messaging.AsyncResult.SyncProcessMessage(IMessage msg)
  bei System.Runtime.Remoting.Messaging.StackBuilderSink.AsyncProcessMessage(IMessage msg, IMessageSink replySink)
  bei System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.DoAsyncCall()
  bei System.Runtime.Remoting.Proxies.AgileAsyncWorkerItem.ThreadPoolCallBack(Object o)
  bei System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
  bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
  bei System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
  bei System.Threading.ThreadPoolWorkQueue.Dispatch()
  bei System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
  </code></pre>