public override HttpActionDescriptor SelectAction(HttpControllerContext controllerContext) { HttpActionDescriptor decriptor = null; try { decriptor = base.SelectAction(controllerContext); } catch (HttpResponseException ex) { var code = ex.Response.StatusCode; if (code != HttpStatusCode.NotFound && code != HttpStatusCode.MethodNotAllowed) { throw; } var routeData = controllerContext.RouteData; routeData.Values["action"] = DefaultActionName; IHttpController httpController = ControllerBuilder(); controllerContext.Controller = httpController; controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, httpController.GetType().Name, httpController.GetType()); decriptor = base.SelectAction(controllerContext); } return(decriptor); }
private static HttpControllerDescriptor BuildHttpControllerDescriptor(IHttpController controller, string controllerName = "AnyController", HttpConfiguration httpConfiguration = null) { HttpControllerDescriptor expectedInner = new Mock <HttpControllerDescriptor>().Object; expectedInner.ControllerName = controllerName; expectedInner.ControllerType = controller.GetType(); expectedInner.Configuration = httpConfiguration ?? new HttpConfiguration(); return(expectedInner); }
public override void OnException(HttpActionExecutedContext actionExecutedContext) { HttpActionContext actionContext = actionExecutedContext.ActionContext; HttpControllerContext controllerContext = actionContext.ControllerContext; Exception oldException = actionExecutedContext.Exception; // Get the name of the action ("Decrypt") and controller ("DocumentController") string actionName = actionContext.ActionDescriptor.ActionName; ServiceException sameException = oldException as ServiceException; if (sameException != null) { // Add the action name. sameException.Action = actionName; try { // Get the value from the attribute on the method. IHttpController controller = controllerContext.Controller; var serviceErrorAttribute = (ServiceErrorAttribute)controller.GetType().GetMethod(actionName).GetCustomAttribute(typeof(ServiceErrorAttribute), false); // Get additional properties sameException.ConsumeAttribute(serviceErrorAttribute); } catch { } actionExecutedContext.Exception = sameException; } else { string userSafeMessage = null; try { // Check the method for an attribute we can use for more information. // Get the value from the attribute on the method. IHttpController controller = controllerContext.Controller; var serviceErrorAttribute = (ServiceErrorAttribute)controller.GetType().GetMethod(actionName).GetCustomAttribute(typeof(ServiceErrorAttribute), false); userSafeMessage = serviceErrorAttribute.Message; ServiceException newException = new ServiceException(userSafeMessage, actionExecutedContext.Exception); newException.Action = actionName; // Get additional properties newException.ConsumeAttribute(serviceErrorAttribute); actionExecutedContext.Exception = newException; } catch { userSafeMessage = "An error occurred. Please contact an administrator."; ServiceException newException = new ServiceException(userSafeMessage, actionExecutedContext.Exception); newException.Action = actionName; actionExecutedContext.Exception = newException; } } }
private static HttpControllerDescriptorTracer GetHttpControllerDescriptorTracer(HttpControllerDescriptor controllerDescriptor, ITraceWriter traceWriter) { return(new HttpControllerDescriptorTracer( configuration: new HttpConfiguration(), controllerName: "AnyController", controllerType: _controller.GetType(), innerDescriptor: controllerDescriptor, traceWriter: traceWriter)); }
void IDisposable.Dispose() { IDisposable disposable = _innerController as IDisposable; if (disposable != null) { _traceWriter.TraceBeginEnd( _request, TraceCategories.ControllersCategory, TraceLevel.Info, _innerController.GetType().Name, DisposeMethodName, beginTrace: null, execute: disposable.Dispose, endTrace: null, errorTrace: null); } }
public void SelectController_Invokes_Inner_And_Traces() { // Arrange Mock <HttpControllerDescriptor> mockControllerDescriptor = new Mock <HttpControllerDescriptor>( _controllerContext.Configuration, "AnyController", _controller.GetType() ); Mock <IHttpControllerSelector> mockSelector = new Mock <IHttpControllerSelector>(); mockSelector .Setup(b => b.SelectController(It.IsAny <HttpRequestMessage>())) .Returns(mockControllerDescriptor.Object); TestTraceWriter traceWriter = new TestTraceWriter(); HttpControllerSelectorTracer tracer = new HttpControllerSelectorTracer( mockSelector.Object, traceWriter ); TraceRecord[] expectedTraces = new TraceRecord[] { new TraceRecord(_request, TraceCategories.ControllersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "SelectController" }, new TraceRecord(_request, TraceCategories.ControllersCategory, TraceLevel.Info) { Kind = TraceKind.End, Operation = "SelectController" } }; // Act HttpControllerDescriptor controllerDescriptor = ( (IHttpControllerSelector)tracer ).SelectController(_request); // Assert Assert.Equal <TraceRecord>( expectedTraces, traceWriter.Traces, new TraceRecordComparer() ); Assert.IsAssignableFrom <HttpControllerDescriptorTracer>(controllerDescriptor); }
IHttpController IHttpControllerActivator.Create( HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType ) { IHttpController controller = null; _traceWriter.TraceBeginEnd( request, TraceCategories.ControllersCategory, TraceLevel.Info, _innerActivator.GetType().Name, CreateMethodName, beginTrace: null, execute: () => { controller = _innerActivator.Create( request, controllerDescriptor, controllerType ); }, endTrace: (tr) => { tr.Message = controller == null ? SRResources.TraceNoneObjectMessage : controller.GetType().FullName; }, errorTrace: null ); if (controller != null && !(controller is HttpControllerTracer)) { controller = new HttpControllerTracer(request, controller, _traceWriter); } return(controller); }
Task <HttpResponseMessage> IHttpController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken) { return(_traceWriter.TraceBeginEndAsync <HttpResponseMessage>( controllerContext.Request, TraceCategories.ControllersCategory, TraceLevel.Info, _innerController.GetType().Name, ExecuteAsyncMethodName, beginTrace: null, execute: () => { // Critical to allow wrapped controller to have itself in ControllerContext controllerContext.Controller = ActualController(controllerContext.Controller); return _innerController.ExecuteAsync(controllerContext, cancellationToken); }, endTrace: (tr, response) => { if (response != null) { tr.Status = response.StatusCode; } }, errorTrace: null)); }
/// <summary> /// Gets logger. /// </summary> /// <param name="controller"> /// The controller. /// </param> /// <returns> /// The <see cref="Logger"/>. /// </returns> private static Logger GetLogger(IHttpController controller) { return LogManager.GetLogger(controller.GetType().FullName); }
public static string ControllerName([NotNull] this IHttpController thisValue) { return(IHttpControllerHelper.ControllerName(thisValue.GetType())); }
private static void SetActionInformation(ServiceError error, HttpActionExecutedContext actionExecutedContext) { // Get the name of the action ("Decrypt") and controller ("DocumentController") HttpActionContext actionContext = actionExecutedContext.ActionContext; IHttpController controller = actionContext.ControllerContext.Controller; string actionName = actionContext.ActionDescriptor.ActionName; try { // Get the value from the attribute on the method. ServiceErrorAttribute serviceErrorAttribute = (ServiceErrorAttribute)GetCustomAttribute(controller.GetType().GetMethod(actionName), typeof(ServiceErrorAttribute), true); if (serviceErrorAttribute != null) { if (serviceErrorAttribute.Message != null) { error.Message = serviceErrorAttribute.Message; } if (serviceErrorAttribute.MethodName != null) { actionName = serviceErrorAttribute.MethodName; } } } catch { } var controllerDescriptor = actionContext.ActionDescriptor.ControllerDescriptor; if (controllerDescriptor != null) { var controllerName = controllerDescriptor.ControllerName; if (!string.IsNullOrEmpty(controllerName) && !string.IsNullOrEmpty(actionName)) { actionName = string.Format("{0}.{1}", controllerName, actionName); } } error.MethodName = actionName; }
private string GetTerminalName(IHttpController curController) { string assemblyName = curController.GetType().Assembly.FullName.Split(new char[] { ',' })[0]; return(assemblyName); }
private static HttpControllerDescriptor BuildHttpControllerDescriptor(IHttpController controller, string controllerName = "AnyController", HttpConfiguration httpConfiguration = null) { HttpControllerDescriptor expectedInner = new Mock<HttpControllerDescriptor>().Object; expectedInner.ControllerName = controllerName; expectedInner.ControllerType = controller.GetType(); expectedInner.Configuration = httpConfiguration ?? new HttpConfiguration(); return expectedInner; }