このQ & Aで、ASP.NET MVC を非同期処理に対応させる方法を 1 つ見つけました。しかし、私はそれを機能させることはできません。
基本的には、メソッドGetHttpHandlerを 1 つだけ持つ IRouteHandler の新しい実装を作成するという考え方です。GetHttpHandlerメソッドは、Begin/EndXXXX パターン API を持っているため、単に ではなく実装を返す必要がありますIHttpAsyncHandler
。IHttpHandler
IHttpAsyncHandler
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();
}
}
}
次に、ファイル Global.asax.cs の RegisterRoutes メソッドで、このクラス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 = "" }),
});
}
ProcessRequest、BeginProcessRequest、EndProcessRequestにブレークポイントを設定しました。ProcessRequestのみが実行されます。つまり、AsyncMvcHandlerはIHttpAsyncHandlerを実装していますが。ASP.NET MVC はそれを認識せず、単に実装として処理します。IHttpHandler
ASP.NET MVCでAsyncMvcHandlerをIHttpAsyncHandlerとして扱うようにして、非同期ページ処理を行うにはどうすればよいですか?