title: Simple Injector async life time scoping and security exception
body: <p>I have a <code>AsyncCommandHandlerDecorator&lt;TCommand&gt;</code> and <code>LifetimeScopedCommandHandlerDecorator&lt;TCommand&gt;</code> as shown below:</p>
  
  <pre><code>public class AsyncCommandHandlerDecorator&lt;TCommand&gt; 
      : ICommandHandler&lt;TCommand&gt; where TCommand : ICommand {
  
      private readonly Func&lt;ICommandHandler&lt;TCommand&gt;&gt; _factory;
  
      public AsyncCommandHandlerDecorator(Func&lt;ICommandHandler&lt;TCommand&gt;&gt; factory) {
          _factory = factory;
      }
  
      [SecurityCritical]
      // with or whitout SecurityCritical attribute, problem exists. 
      public void Handle(TCommand command) {
          ThreadPool.QueueUserWorkItem(_ =&gt; {
              var handler = _factory();
              handler.Handle(command);
          });
      }
  }
  
  // AND
  
  public class LifetimeScopedCommandHandlerDecorator&lt;TCommand&gt;
      : ICommandHandler&lt;TCommand&gt; where TCommand : ICommand {
  
      private readonly Container _container;
      private readonly Func&lt;ICommandHandler&lt;TCommand&gt;&gt; _factory;
  
      public LifetimeScopedCommandHandlerDecorator(Container container,
          Func&lt;ICommandHandler&lt;TCommand&gt;&gt; factory) {
          _container = container;
          _factory = factory;
      }
  
  
      [SecurityCritical]
      // with or whitout SecurityCritical attribute, problem exists. 
      public void Handle(TCommand command) {
          using (_container.BeginLifetimeScope()) {
              var handler = _factory();
              handler.Handle(command);
          }
      }
  }
  </code></pre>
  
  <p>and I register decorators like this:</p>
  
  <pre><code>container.RegisterSingleDecorator(
      typeof(ICommandHandler&lt;&gt;),
      typeof(LifetimeScopedCommandHandlerDecorator&lt;&gt;),
      c =&gt; c.ImplementationType.Name.StartsWith("Async"));
  
  container.RegisterSingleDecorator(
      typeof(ICommandHandler&lt;&gt;),
      typeof(AsyncCommandHandlerDecorator&lt;&gt;),
      c =&gt; c.ImplementationType.Name.StartsWith("Async"));
  </code></pre>
  
  <p>But, when the async process attempt to invoke, I get this error:</p>
  
  <blockquote>
    <p>Attempt by security transparent method
    'LifetimeScopedCommandHandlerDecorator`1.Handle(!0)' to
    access security critical method
    'SimpleInjector.SimpleInjectorLifetimeScopeExtensions.BeginLifetimeScope(SimpleInjector.Container)'
    failed.</p>
  </blockquote>
  
  <p>I tried to use <code>SecurityCritical</code> attribute on both <code>LifetimeScopedCommandHandlerDecorator.Handle</code> and <code>AsyncCommandHandlerDecorator.Handle</code>, but it didn't help. Have you any idea please?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>The exception is of type <code>MethodAccessException</code> and here is the complete stack trace:</p>
  
  <blockquote>
    <p>at MYNAMESPACE.LifetimeScopedCommandHandlerDecorator`1.Handle(TCommand
    command) </p>
    
    <p>at MYNAMESPACE.AsyncCommandHandlerDecorator`1.&lt;>c_<em>DisplayClass1.b</em>_0(Object
    _)     </p>
  
  <pre><code>   in C:\MYPROJECTPATH\AsyncCommandHandlerDecorator.cs:line 23 
  </code></pre>
    
    <p>at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object
    state) </p>
    
    <p>at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) </p>
    
    <p>at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) </p>
    
    <p>at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() </p>
    
    <p>at System.Threading.ThreadPoolWorkQueue.Dispatch() </p>
    
    <p>at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()</p>
  </blockquote>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>With or whitout <code>SecurityCritical</code> attribute, problem exists. </p>