title: How to use Castle Windsor's PerWebRequest lifestyle with OWIN
body: <p>I am converting an existing ASP .Net Web API 2 project to use OWIN. The project uses Castle Windsor as the dependency injection framework with one of the dependencies set to use the PerWebRequest lifestyle.</p>
  
  <p>When I make a request to the server I get a <code>Castle.MicroKernel.ComponentResolutionException</code> exception. The exception recommends adding the following to the <code>system.web/httpModules</code> and <code>system.WebServer/modules</code> sections in the config file:</p>
  
  <pre><code>&lt;add name="PerRequestLifestyle"
       type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" /&gt;
  </code></pre>
  
  <p>This doesn't resolve the error.</p>
  
  <p>Taking inspiration from the example provided by SimpleInjector's OWIN integration, I attempted to set a scope in the OWIN startup class (as well as update the dependency's lifestyle) using:</p>
  
  <pre><code>appBuilder.User(async (context, next) =&gt;
  {
      using (config.DependencyResolver.BeginScope()){
      {
          await next();
      }
  }
  </code></pre>
  
  <p>Unfortunately this hasn't worked either.</p>
  
  <p>How can I use Castle Windsor's PerWebRequest lifestyle or simulate it in OWIN?</p>