title: Castle.Windsor - HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net - called via async, definitely from controller
body: <p>I'm using <code>PerWebRequestLifestyle</code> with Castle.Windsor, and getting:</p>
  
  <p><code>HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net</code></p>
  
  <p>It's a long stack trace, but it does definitely start in a controller.  The stack trace for the exception doesn't show that, it shows the command handler which is invoked from the controller asynchronously.</p>
  
  <p>Why might this be?</p>
  
  <p>Exception stack trace:</p>
  
  <pre><code>   at Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule.GetScope()
     at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.GetScope(CreationContext context)
     at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
     at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden&amp; burden)
     at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
     at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
     at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context)
     at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context)
     at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
     at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden)
     at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally)
     at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.&lt;&gt;c__DisplayClass4_0.&lt;Resolve&gt;b__0(Action`1 afterCreated)
     at Castle.MicroKernel.Lifestyle.Scoped.CallContextLifetimeScope.GetCachedInstance(ComponentModel model, ScopedInstanceActivationCallback createInstance)
     at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
     at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden&amp; burden)
     at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
     at Castle.MicroKernel.Resolvers.DefaultDependencyResolver.Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, DependencyModel dependency)
     at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateConstructorArguments(ConstructorCandidate constructor, CreationContext context)
     at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.Instantiate(CreationContext context)
     at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.InternalCreate(CreationContext context)
     at Castle.MicroKernel.ComponentActivator.AbstractComponentActivator.Create(CreationContext context, Burden burden)
     at Castle.MicroKernel.Lifestyle.AbstractLifestyleManager.CreateInstance(CreationContext context, Boolean trackedExternally)
     at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.&lt;&gt;c__DisplayClass4_0.&lt;Resolve&gt;b__0(Action`1 afterCreated)
     at Castle.MicroKernel.Lifestyle.Scoped.CallContextLifetimeScope.GetCachedInstance(ComponentModel model, ScopedInstanceActivationCallback createInstance)
     at Castle.MicroKernel.Lifestyle.ScopedLifestyleManager.Resolve(CreationContext context, IReleasePolicy releasePolicy)
     at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden&amp; burden)
     at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
     at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
     at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
     at Castle.Windsor.WindsorContainer.Resolve[T]()
     at RedactedCustomer.MyFrameworkTech.TriggerableFactory.Create[TTriggerType]() in C:\BuildAgent\work\6c87fea77042db3f\backend\RedactedCustomer.MyFrameworkTech\TriggerableFactory.cs:line 17
     at Redacted.MyFramework.Workflow.TriggerExecutor.&lt;LockAndExecuteAsync&gt;d__13`2.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Redacted.MyFramework.Workflow.TriggerExecutor.&lt;ExecuteSingleAsync&gt;d__12`2.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at Redacted.MyFramework.Workflow.TriggerExecutor.&lt;ExecuteSingleAsync&gt;d__12`2.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Redacted.MyFramework.Workflow.WorkflowExecutionContext.&lt;ExecuteCapturedActionsAsync&gt;d__8.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Redacted.MyFramework.Workflow.MultiAsyncWorkflowExecutionContext.&lt;ExecuteCapturedActionsAsync&gt;d__9.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at Redacted.MyFramework.Workflow.TriggerExecutor.&lt;ExecuteSingleAsync&gt;d__12`2.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
     at RedactedCustomer.Core.BusinessLogic.CommandHandlers.MyCommandHandler.&lt;ExecuteAsync&gt;d__12.MoveNext() in C:\BuildAgent\work\6c87fea77042db3f\backend\core\RedactedCustomer.Core.BusinessLogic\CommandHandlers\MyCommandHandler.cs:line 282
  </code></pre>