Wie man ASP.NET MVC IHttpAsyncHandler von IRouteHandler.GetHttpHandler erkennen ()?
-
07-07-2019 - |
Frage
In diesem Frage & Antwort , fand ich eine Art und Weise ASP.NET MVC unterstützt asynchrone Verarbeitung zu machen. Allerdings kann ich nicht arbeiten.
Im Grunde ist die Idee, eine neue Implementierung von IRouteHandler zu schaffen, das nur eine Methode hat GetHttpHandler . Die GetHttpHandler Methode sollte anstatt nur IHttpHandler eine IHttpAsyncHandler Implementierung zurückkehren, weil IHttpAsyncHandler hat Begin / EndXXXX Muster 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();
}
}
}
Dann wird in der RegisterRoutes Methode der Datei Global.asax.cs, registriert diese Klasse AsyncMvcRouteHandler . public static void RegisterRoutes (Routecollection Routen) { routes.IgnoreRoute ( "{} Ressource .axd / {* pathInfo}");
routes.Add(new Route("{controller}/{action}/{id}", new AsyncMvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "Home", action = "Index", id = "" }),
});
}
Ich setze Haltepunkt an Process , BeginProcessRequest und EndProcessRequest . Nur Process ausgeführt wird. In einem anderen Wort, obwohl AsyncMvcHandler Geräte IHttpAsyncHandler . ASP.NET MVC nicht weiß, dass und zu handhaben es nur als IHttpHandler Umsetzung.
Wie ASP.NET MVC zu behandeln AsyncMvcHandler IHttpAsyncHandler , damit wir haben asynchrone Seitenverarbeitung machen?
Lösung 2
Nach Stunden des Ärgers mit dem Code, fand ich das Problem aus.
In meinem Visual Studio 2008, wenn ich Strg + F5 drücken, wird die Application Development Server gestartet und IE ausgeklappt ist für den Zugriff auf " http: // localhost: 3573 / “. In diesem Fall wird der Sync-API Process wird aufgerufen. Der Stack-Trace ist wie diese.
MyMvcApplication.DLL! MyMvcApplication.AsyncMvcRouteHandler.AsyncMvcHandler.ProcessRequest (System.Web.HttpContext = Httpcontext {System.Web.HttpContext}) Zeile 59 C # System.Web.Mvc.dll! System.Web.Mvc.MvcHttpHandler.VerifyAndProcessRequest (System.Web.IHttpHandler Httphandler, System.Web.HttpContextBase Httpcontext) + 0x19 Bytes
System.Web.Routing.dll! System.Web.Routing.UrlRoutingHandler.ProcessRequest (System.Web.HttpContextBase Httpcontext) + 0x66 Bytes
System.Web.Routing.dll! System.Web.Routing.UrlRoutingHandler.ProcessRequest (System.Web.HttpContext Httpcontext) + 0x28 Bytes
System.Web.Routing.dll! System.Web.Routing.UrlRoutingHandler.System.Web.IHttpHandler.ProcessRequest (System.Web.HttpContext Kontext) + 0x8 Bytes
MyMvcApplication.DLL! MyMvcApplication._Default.Page_Load (Objekt sender = {ASP.default_aspx}, System.EventArgs e = {System.EventArgs}) Linie 13 + 0x1a Bytes C #
Allerdings, wenn ich die URL im Internet Explorer ändern zu sein „ http: // localhost: 3573 / whatever.mvc “, trifft es die BeginProcessRequest . Der Stack-Trace ist wie diese.
MyMvcApplication.DLL! MyMvcApplication.AsyncMvcRouteHandler.AsyncMvcHandler.BeginProcessRequest (System.Web.HttpContext context = {System.Web.HttpContext}, System.AsyncCallback cb = {Method = {Leere OnAsyncHandlerCompletion (System.IAsyncResult)}}, Objekt Extradata = null) Zeile 66 C # 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 Schritt = {System.Web.HttpApplication.CallHandlerExecutionStep}, ref bool completedSynchronously = true) + 0x9C Bytes
System.Web.dll! System.Web.HttpApplication.ApplicationStepManager.ResumeSteps (System.Exception Fehler) + 0x133 Bytes
System.Web.dll! System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest (System.Web.HttpContext Kontext, System.AsyncCallback cb, Objekt Extradata) + 0x7c Bytes
System.Web.dll! System.Web.HttpRuntime.ProcessRequestInternal (System.Web.HttpWorkerRequest wr = {Microsoft.VisualStudio.WebHost.Request}) + 0x17c Bytes System.Web.dll! System.Web.HttpRuntime.ProcessRequestNoDemand (System.Web.HttpWorkerRequest wr) + 0x63 Bytes
System.Web.dll! System.Web.HttpRuntime.ProcessRequest (System.Web.HttpWorkerRequest wr) + 0x47 Bytes
WebDev.WebHost.dll! Microsoft.VisualStudio.WebHost.Request.Process () + 0xF1 Bytes WebDev.WebHost.dll! Microsoft.VisualStudio.WebHost.Host.ProcessRequest (Microsoft.VisualStudio.WebHost.Connection conn) + 0x4E Bytes
Es scheint, dass nur URL mit ".mvc" Suffix asynchroner API aufgerufen machen kann.
Andere Tipps
Ich hatte das gleiche Problem, aber ich fand, dass es, weil mein fängt alle Routen-Handler war:
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index" }
);
Wurde die Anforderung, nicht die individuellen Route Kommissionierung bis ich fügte hinzu, dass mit den Asynchron-Route-Handler behandelt. Vielleicht durch die .mvc in Ihrer benutzerdefinierten Route defintion mit erstellt man einen Unterschied, so dass es eher als die synchronen catch-all verwendet wurde.
Ich habe versucht, dies in der Vergangenheit zu tun, schaffe ich es entweder um die Ansicht zu bekommen zu machen und dann alle Asynchron-Aufgaben würden beenden. Oder die Asynchron-Aufgaben zu beenden, aber die Aussicht nicht machen.
Ich habe ein RouteCollectionExtensions basiert auf dem ursprünglichen MVC-Code. In meinem AsyncMvcHandler hatte ich eine leere Methode (keine Ausnahme) für ProcessMethod.