问题
We are running an Owin applications on IIS 8.5 on Win 2012 R2 behind a loadbalancer. One some occations, requests to certain URLs hang indefinitely. If the user selects cancel in the browser, and reloads the page, everything is OK and the server responds quickly.
In IIS manager, we can see the requests hanging: The hang seems to occur inside the Owin pipeline. We are running .NET 4.5.2 and the latest Owin packages.
Here's the code for the /api/whoami
endpoint:
[Authorize]
public class WhoAmIController : ApiController
{
[Route("whoami")]
[HttpGet]
public IHttpActionResult Whoami()
{
return Json(ClaimsPrincipal.Current.Identity.Name);
}
}
An observation is that requests to this endpoint hangs in the AuthenticateRequest
stage in the IIS pipeline, not PreExecuteRequestHandler
which is the default IIS stage for the OWIN pipeline. Our only authentication method is cookie authentication, which executes in the AuthenticateRequest
stage.
Any ideas?
Edit: adjusted screenshot
回答1:
The problem was that we had code executing on the IIS event PreSendRequestHeaders
, which apperently is bad according to
http://www.asp.net/aspnet/overview/web-development-best-practices/what-not-to-do-in-aspnet,-and-what-to-do-instead#presend
Our intention was to adjust HTTP headers on the way out on all request. The fix was to move the code to the BeginRequest
event.
回答2:
Have you tried adding .ConfigureAwait(false)
to your async calls?
Doing so will make the continuation continue on the "correct" thread.
来源:https://stackoverflow.com/questions/33256407/owin-on-iis-web-requests-hang-indefinitely