title: Owin self hosting - limiting maximum time for request
body: <p>How to efficiently limit request length timeout on server side ? I'm using <code>Microsoft.Owin.Host.HttpListener</code> and there are cases when (due to call to external service) serving request takes ridiculous amount of time. This is not a problem - but web server should give up sooner than - well never (I did some tests, but after 5 minutes I stopped it).</p>
  
  <p>Is there a way how to limit time for serving single request (similar to <code>&lt;httpRuntime maxRequestLength="..." /&gt;</code> in IIS ecosystem) ?</p>
  
  <p>Sample controller code:</p>
  
  <pre><code>public async Task&lt;HttpResponseMessage&gt; Get() {
    // ... calls to 3pty services here
     await Task.Delay(TimeSpan.FromMinutes(5));
  }
  </code></pre>
  
  <p>Starting web server:</p>
  
  <pre><code>WebApp.Start(this.listeningAddress, new Action&lt;IAppBuilder&gt;(this.Build));
  </code></pre>
  
  <p>Note: I've read about limiting http listener, but that just limits incoming request properties, it doesn't cancel request that is slow due to slow server processing:</p>
  
  <pre><code>var listener = appBuilder.Properties[typeof(OwinHttpListener).FullName] as OwinHttpListener;
  var timeoutManager = listener.Listener.TimeoutManager;
  timeoutManager.DrainEntityBody = TimeSpan.FromSeconds(20);
  timeoutManager.EntityBody = TimeSpan.FromSeconds(20);
  timeoutManager.HeaderWait = TimeSpan.FromSeconds(20);
  timeoutManager.IdleConnection = TimeSpan.FromSeconds(20);
  timeoutManager.RequestQueue = TimeSpan.FromSeconds(20);
  </code></pre>
  
  <p>Related:</p>
  
  <p><a href="https://github.com/aspnet/AspNetKatana/issues/152" rel="nofollow noreferrer">https://github.com/aspnet/AspNetKatana/issues/152</a></p>