/* * In previous practice. We refactored InvokeAction method. Now we have * renamed its name from "InvokeAction" to "InvokeActionInternal". The * InvokeAction method will no longer accept ActionDescriptor instance. * Instead, it will create the ActionDescriptor instance from a matching * route and an IDependencyResolver instance. * * The matched route contains the type of the controller, the name of the * action and the method constraint. It should use a resolver to create * controller from its type. */ public static HttpResponseMessage InvokeAction(HttpRoute matchedRoute, IDependencyResolver resolver) { var controller = resolver.GetService(matchedRoute.ControllerType) as HttpController; if (controller == null) { return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } return(InvokeActionInternal(new ActionDescriptor(controller, matchedRoute.ActionName, matchedRoute.MethodConstraint))); }
/* * In previous practice. We refactored InvokeAction method. Now we have * renamed its name from "InvokeAction" to "InvokeActionInternal". The * InvokeAction method will no longer accept ActionDescriptor instance. * Instead, it will create the ActionDescriptor instance from a matching * route and an IDependencyResolver instance. * * The matched route contains the type of the controller, the name of the * action and the method constraint. It should use a resolver to create * controller from its type. */ public static HttpResponseMessage InvokeAction(HttpRoute matchedRoute, IDependencyResolver resolver) { Type matchedRouteControllerType = matchedRoute.ControllerType; var activationContext = (HttpController)resolver.GetService(matchedRouteControllerType); if (activationContext == null) { return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } return(InvokeActionInternal(new ActionDescriptor(activationContext, matchedRoute.ActionName, matchedRoute.MethodConstraint))); }
protected override async Task <HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { HttpRoute routeData = configuration.Routes.GetRouteData(request); if (routeData == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } return(ControllerActionInvoker.InvokeAction(routeData, configuration.CachedControllerTypes, configuration.DependencyResolver, configuration.ControllerFactory)); }
public static Task <HttpResponseMessage> InvokeAction( HttpRequestMessage request) { HttpController controller; HttpRequestContext context = request.GetRequestContext(); if (context == null) { throw new InvalidOperationException("Cannot find request context"); } HttpRoute matchedRoute = context.MatchedRoute; HttpConfiguration configuration = context.Configuration; #region Please modify the following code /* * A dependency scope will be generated for each request. And it will manage the * lifetime scopes for all items created during this request. */ IDependencyScope scope = context.GetDependencyScope(); #endregion try { controller = configuration.ControllerFactory.CreateController( matchedRoute.ControllerName, configuration.CachedControllerTypes, scope); if (controller == null) { return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound))); } controller.Request = request; } catch (ArgumentException) { return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.InternalServerError))); } return(InvokeActionInternal( new ActionDescriptor( controller, matchedRoute.ActionName, matchedRoute.MethodConstraint))); }
protected override Task <HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { HttpRoute route = configuration.Routes.GetRouteData(request); if (route == null) { return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound))); } HttpResponseMessage response = ControllerActionInvoker.InvokeAction( route, configuration.CachedControllerTypes, configuration.DependencyResolver, configuration.ControllerFactory); return(Task.Run(() => response, cancellationToken)); }
protected override async Task <HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { HttpRoute matchedRoute = configuration.Routes.GetRouteData(request); if (matchedRoute == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } try { request.SetRequestContext(configuration, matchedRoute); return(await ControllerActionInvoker.InvokeAction(request)); } catch (Exception) { return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } }
/* * You should create a request context to hold all the readonly information * needed for processing a request. The HttpRequestMessage has a greate property * called Properties to hold additional information, which will help you. You * should use requestContextKey to store and retrive the request context. */ public static void SetRequestContext( this HttpRequestMessage request, HttpConfiguration configuration, HttpRoute matchedRoute) { if (request == null) { throw new ArgumentNullException(nameof(request)); } if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } if (matchedRoute == null) { throw new ArgumentNullException(nameof(matchedRoute)); } request.Properties.Add(requestContextKey, new HttpRequestContext(configuration, matchedRoute)); }
public static HttpResponseMessage InvokeAction( HttpRoute matchedRoute, ICollection <Type> controllerTypes, IDependencyResolver resolver, IControllerFactory controllerFactory) { #region Please modify the following code to pass the test /* * In this test, you have to create the controller from its name rather * than its type. So we introduced a new interface called IControllerFactory. * It will create controller directly by its name with the help of * controller type collection returned by IHttpControllerTypeResolver and * IDependencyResolver. */ return(InvokeActionInternal( new ActionDescriptor(null, matchedRoute.ActionName, matchedRoute.MethodConstraint))); #endregion }
public static Task <HttpResponseMessage> InvokeAction( HttpRequestMessage request) { HttpController controller; HttpRequestContext context = request.GetRequestContext(); if (context == null) { throw new InvalidOperationException("Cannot find request context"); } HttpRoute matchedRoute = context.MatchedRoute; HttpConfiguration configuration = context.Configuration; try { controller = configuration.ControllerFactory.CreateController( matchedRoute.ControllerName, configuration.CachedControllerTypes, configuration.DependencyResolver); if (controller == null) { return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.NotFound))); } controller.Request = request; } catch (ArgumentException) { return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.InternalServerError))); } return(InvokeActionInternal( new ActionDescriptor( controller, matchedRoute.ActionName, matchedRoute.MethodConstraint))); }
protected override async Task <HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { HttpRoute matchedRoute = configuration.Routes.GetRouteData(request); if (matchedRoute == null) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); } #region Please dispose the request context after request processing /* * Please find a place to dispose the request context after request processing. */ try { request.SetRequestContext(configuration, matchedRoute); HttpResponseMessage response = await ControllerActionInvoker.InvokeAction(request); return(response); } catch (Exception) { return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } finally { request.DisposeRequestContext(); } #endregion }
public HttpRequestContext(HttpConfiguration configuration, HttpRoute matchedRoute) { Configuration = configuration; MatchedRoute = matchedRoute; }
/* * In previous practice. We refactored InvokeAction method. Now we have * renamed its name from "InvokeAction" to "InvokeActionInternal". The * InvokeAction method will no longer accept ActionDescriptor instance. * Instead, it will create the ActionDescriptor instance from a matching * route and an IDependencyResolver instance. * * The matched route contains the type of the controller, the name of the * action and the method constraint. It should use a resolver to create * controller from its type. */ public static HttpResponseMessage InvokeAction(HttpRoute matchedRoute, IDependencyResolver resolver) { return(InvokeActionInternal(new ActionDescriptor(null, matchedRoute.ActionName, matchedRoute.MethodConstraint))); }