title: Correctly set Scope when using WebApi, SimpleInjector and MediatR
body: <p><strong>Controller</strong></p>
  <pre><code>public class LocationsController : ApiController
  {
      private readonly IMediator _mediator;
  
      public LocationsController(IMediator mediator)
      {
          _mediator = mediator;
      }
  
      public IEnumerable&lt;Place&gt; Get()
      {
          return _mediator.Send(new GetLatestMapData&lt;Place&gt;());
      }
  }
  </code></pre>
  <p>On first request of Get() action, the Handler is instantiated by SimpleInjector and executed correctly.</p>
  <p>On the second request (F5 in browser for e.g.), it fails with :</p>
  <blockquote>
  <p>Handler was not found for request of type ....</p>
  <p>Container or service locator not configured properly or handlers not registered with your container.</p>
  </blockquote>
  <p>and inner exception of:</p>
  <blockquote>
  <p>Cannot access a disposed object.</p>
  <p>Object name: 'The ThreadLocal object has been disposed.'</p>
  </blockquote>
  <p><strong>OWIN Startup</strong></p>
  <pre><code>public class Startup
  {
      public void Configuration(IAppBuilder app)
      {
          // SimpleInjector
          var container = CompositionRoot.CreateContainer();
  
          var config = GlobalConfiguration.Configuration;
  
          config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
  
          // Routing
          config.MapHttpAttributeRoutes();
  
          config.Routes.MapHttpRoute(&quot;DefaultApi&quot;, &quot;api/{controller}/{id}&quot;,
              new { id = RouteParameter.Optional });
  
          config.EnsureInitialized();
  
          app.UseWebApi(config);
      }
  }
  </code></pre>
  <p><strong>SimpleInjector IPackage for WebAPI project</strong></p>
  <pre><code>public class Installer : IPackage
  {
      public void RegisterServices(Container c)
      {
          c.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();
  
          c.RegisterWebApiControllers(GlobalConfiguration.Configuration);
      }
  }
  </code></pre>
  <p>I think what's happening is the Handler is correctly created, and then disposed after the first request.
  Now, I don't know why, but on subsequent requests, the Handler isn't re-created. I know this because if I change the WebApiRequestLifestyle to 'not dispose when scope ends', it works for every request:</p>
  <pre><code>c.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle(false
  /*disposeInstanceWhenScopeEnds*/);
  </code></pre>
  <p><strong>Questions</strong></p>
  <ol>
  <li>Should I keep the <code>disposeInstanceWhenScopeEnds</code> parameter set to false?</li>
  <li>If not, what is the correct solution?</li>
  <li>I see this has been <a href="https://stackoverflow.com/questions/38615432/dependency-scope-issues-with-mediatr-and-simpleinjector">solved before by creating a LifetimeScopeDecorator</a>... however, surely this functionality is already provided by the SimpleInjector WebApi integration library? What am I missing?</li>
  </ol>
  <p>(And thank you for reading)</p>