In this question & answer, I found one way to make ASP.NET MVC support asynchronous processing. However, I cannot make it work.
Basically, the idea is to create a new implementation of IRouteHandler which has only one method GetHttpHandler. The GetHttpHandler method should return an IHttpAsyncHandler
implementation instead of just IHttpHandler
, because IHttpAsyncHandler
has Begin/EndXXXX pattern API.
public class AsyncMvcRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new AsyncMvcHandler(requestContext);
}
class AsyncMvcHandler : IHttpAsyncHandler, IRequiresSessionState
{
public AsyncMvcHandler(RequestContext context)
{
}
// IHttpHandler members
public bool IsReusable { get { return false; } }
public void ProcessRequest(HttpContext httpContext) { throw new NotImplementedException(); }
// IHttpAsyncHandler members
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
throw new NotImplementedException();
}
public void EndProcessRequest(IAsyncResult result)
{
throw new NotImplementedException();
}
}
}
Then, in the RegisterRoutes method of file Global.asax.cs, register this class AsyncMvcRouteHandler.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(new Route("{controller}/{action}/{id}", new AsyncMvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }),
});
}
I set breakpoint at ProcessRequest, BeginProcessRequest and EndProcessRequest. Only ProcessRequest is executed. In another word, even though AsyncMvcHandler implements IHttpAsyncHandler. ASP.NET MVC doesn't know that and just handle it as an IHttpHandler
implementation.
How to make ASP.NET MVC treat AsyncMvcHandler as IHttpAsyncHandler so we can have asynchronous page processing?
After hours of hassle with the code, I found out the issue.
In my Visual Studio 2008, when I press Ctrl+F5, the Application Development Server is launched and IE is popped up to access "http://localhost:3573/". In this case, the sync API ProcessRequest is invoked. The stack trace is like this.
However, when I change the URL in IE to be "http://localhost:3573/whatever.mvc", it hits the BeginProcessRequest. The stack trace is like this.
It seems that only url with ".mvc" suffix can make asynchronous API invoked.