How to make ASP.NET MVC recognize IHttpAsyncHandler from IRouteHandler.GetHttpHandler ()?

In this question and answer, I found one way to do ASP.NET MVC asynchronous processing. However, I cannot get it to work.

Basically, the idea is to create a new implementation of IRouteHandler that has only one GetHttpHandler method . The GetHttpHandler method should return an implementation of IHttpAsyncHandler, not just IHttpHandler, because IHttpAsyncHandler has a Begin / EndXXXX 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();
        }
    }
}

RegisterRoutes Global.asax.cs AsyncMvcRouteHandler.       public static void RegisterRoutes ( RouteCollection)       {           routes.IgnoreRoute( "{}.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, ?

+3
3

.

Visual Studio 2008, Ctrl + F5, IE, http://localhost:3573/". sync API ProcessRequest. .

MyMvcApplication.DLL! MyMvcApplication.AsyncMvcRouteHandler.AsyncMvcHandler.ProcessRequest(System.Web.HttpContext   httpContext =   {System.Web.HttpContext}) 59 #     System.Web.Mvc.dll! System.Web.Mvc.MvcHttpHandler.VerifyAndProcessRequest(System.Web.IHttpHandler   HttpHandler,   System.Web.HttpContextBase   httpContext) + 0x19
    System.Web.Routing.dll! System.Web.Routing.UrlRoutingHandler.ProcessRequest(System.Web.HttpContextBase   httpContext) + 0x66
    System.Web.Routing.dll! System.Web.Routing.UrlRoutingHandler.ProcessRequest(System.Web.HttpContext   httpContext) + 0x28
    System.Web.Routing.dll! System.Web.Routing.UrlRoutingHandler.System.Web.IHttpHandler.ProcessRequest(System.Web.HttpContext   ) + 0x8
    MyMvcApplication.DLL! MyMvcApplication._Default.Page_Load (   sender = {ASP.default_aspx},   System.EventArgs e =   {System.EventArgs}) 13 + 0x1a    #

, URL IE " http://localhost:3573/whatever.mvc, BeginProcessRequest. .

MyMvcApplication.DLL! MyMvcApplication.AsyncMvcRouteHandler.AsyncMvcHandler.BeginProcessRequest(System.Web.HttpContext   context = {System.Web.HttpContext},   System.AsyncCallback cb = { =   {   OnAsyncHandlerCompletion (System.IAsyncResult)}},    extraData = null) 66 #     System.Web.dll! System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()   + 0x249 bytes System.Web.dll! System.Web.HttpApplication.ExecuteStep(System.Web.HttpApplication.IExecutionStep   step =   {} System.Web.HttpApplication.CallHandlerExecutionStep,   ref bool completedSynchronously =   true) + 0x9c
    System.Web.dll! System.Web.HttpApplication.ApplicationStepManager.ResumeSteps(System.Exception   ) + 0x133
    System.Web.dll! System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext   , System.AsyncCallback cb,    extraData) + 0x7c
    System.Web.dll! System.Web.HttpRuntime.ProcessRequestInternal(System.Web.HttpWorkerRequest   wr =   {} Microsoft.VisualStudio.WebHost.Request)   + 0x17c System.Web.dll! System.Web.HttpRuntime.ProcessRequestNoDemand(System.Web.HttpWorkerRequest   wr) + 0x63
    System.Web.dll! System.Web.HttpRuntime.ProcessRequest(System.Web.HttpWorkerRequest   wr) + 0x47
    WebDev.WebHost.dll! Microsoft.VisualStudio.WebHost.Request.Process()   + 0xf1 . WebDev.WebHost.dll! Microsoft.VisualStudio.WebHost.Host.ProcessRequest(Microsoft.VisualStudio.WebHost.Connection   conn) + 0x4

, url .mvc API.

+2

, , , :

routes.MapRoute(
    "Default",                                                  
    "{controller}/{action}",                           
    new { controller = "Home", action = "Index" }  
);

, , , . , .mvc , , catch-all.

+3

, , async . async , .

RouteCollectionExtensions MVC. AsyncMvcHandler ( ) ProcessMethod.

0

Source: https://habr.com/ru/post/1699341/


All Articles