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 a 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?