title: Retaining principal inside queued background work item
body: <p>I'm using ASP.Net Web API 2 / .Net 4.5.2.  </p>
  
  <p>I'm trying to retain the calling principal when queueing a background work item.  To that end, I'm trying to:</p>
  
  <pre><code>Thread.CurrentPrincipal = callingPrincipal;
  </code></pre>
  
  <p>But when I do so, I get an ObjectDisposedException:</p>
  
  <blockquote>
    <p>System.ObjectDisposedException: Safe handle has been closed</p>
  </blockquote>
  
  <p>How do I keep the current principal inside the background work item?<br>
  Can I make a copy of the principal somehow?</p>
  
  <pre><code>public void Run&lt;T&gt;(Action&lt;T&gt; action)
  {
      _logger.Debug("Queueing background work item");
      var callingPrincipal = Thread.CurrentPrincipal;
      HostingEnvironment.QueueBackgroundWorkItem(token =&gt;
      {
          try
          {
              // UNCOMMENT - THROWS EXCEPTION
              // Thread.CurrentPrincipal = callingPrincipal;
              _logger.Debug("Executing queued background work item");
              using (var scope = DependencyResolver.BeginLifetimeScope())
              {
                  var service = scope.Resolve&lt;T&gt;();
                  action(service);
              }
          }
          catch (Exception ex)
          {
              _logger.Fatal(ex);
          }
          finally
          {
              _logger.Debug("Completed queued background work item");
          }
      });
  }
  </code></pre>