title: What risks does WebApi incur when calling Request.CreateResponse() inside await block?
body: <p>I have an async WebApi controller that throws a StructureMapException when calling <code>Request.CreateResponse(HttStatusCode.Created, result)</code> inside an await block, but only during the first request. StructureMap resolves all the constructor dependencies properly but behaves as though <code>Request.CreateResponse</code> is called after the request ends. Is it simply a bad idea to use <code>Request.CreateResponse</code> inside an await block? What other risks might this strategy incur?  </p>
  
  <p>I'm using the following NuGet packages:</p>
  
  <ul>
  <li>StructureMap 3.1.6.186</li>
  <li>StructureMap.Web 3.1.0.133</li>
  <li>StructureMap.WebApi2 3.0.4.125</li>
  <li>WebActivator 2.0.5</li>
  </ul>
  
  <p>Here is the code:</p>
  
  <pre><code>public class EmailController : ApiController
  {
      private readonly ISaveEmailNotification _saveEmailNotification;
      private readonly ISaveFile _saveFile;
  
      public EmailController(ISaveEmailNotification saveEmailNotification, ISaveFile saveFile)
      {
          _saveEmailNotification = saveEmailNotification;
          _saveFile = saveFile;
      }
  
      [HttpPost]
      public async Task&lt;HttpResponseMessage&gt; SaveEmail(NotificationRequest model)
      {
          return await Task.Run(() =&gt;
          {
              var emailResult = _saveEmailNotification.Execute(model);
              var fileResult = _saveFile.Execute(model, emailResult);
              return Request.CreateResponse(HttpStatusCode.Created, fileResult);
          }
      }
  }
  </code></pre>
  
  <p>Here is the error:</p>
  
  <pre><code>StructureMap.StructureMapException was unhandled by user code
  HResult=-2146233088
  Message=You cannot use the HttpContextLifecycle outside of a web request. Try the HybridLifecycle instead.
  1.) Container.GetInstance(System.Web.HttpContextBase)
  2.) Container.TryGetInstance(System.Web.HttpContextBase)
  
  Source=StructureMap.Web
  Title=You cannot use the HttpContextLifecycle outside of a web request. Try the HybridLifecycle instead.
  StackTrace:
     at StructureMap.Web.Pipeline.HttpContextLifecycle.findHttpDictionary() in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap.Web\Pipeline\HttpContextLifecycle.cs:line 57
     at StructureMap.Web.Pipeline.HttpContextLifecycle.FindCache(ILifecycleContext context) in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap.Web\Pipeline\HttpContextLifecycle.cs:line 20
     at StructureMap.BuildSession.ResolveFromLifecycle(Type pluginType, Instance instance) in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\BuildSession.cs:line 102
     at StructureMap.SessionCache.GetObject(Type pluginType, Instance instance, ILifecycle lifecycle) in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\SessionCache.cs:line 88
     at StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph) in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\SessionCache.cs:line 66
     at StructureMap.Container.GetInstance(Type pluginType) in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\Container.cs:line 335
     at StructureMap.Container.TryGetInstance(Type pluginType) in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\Container.cs:line 278
     at StructureMap.Container.TryGetInstance[T]() in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\Container.cs:line 289
     at NotificationHubWeb.DependencyResolution.StructureMapDependencyScope.get_HttpContext() in C:\Users\Dave\Source\Workspaces\NotificationHub\Main\NotificationHubWeb\DependencyResolution\StructureMapDependencyScope.cs:line 68
     at NotificationHubWeb.DependencyResolution.StructureMapDependencyScope.get_CurrentNestedContainer() in C:\Users\Dave\Source\Workspaces\NotificationHub\Main\NotificationHubWeb\DependencyResolution\StructureMapDependencyScope.cs:line 55
     at NotificationHubWeb.DependencyResolution.StructureMapDependencyScope.DoGetInstance(Type serviceType, String key) in C:\Users\Dave\Source\Workspaces\NotificationHub\Main\NotificationHubWeb\DependencyResolution\StructureMapDependencyScope.cs:line 109
     at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 49
  </code></pre>