Пример #1
0
 /// <summary>
 /// 执行Api行为
 /// </summary>   
 /// <param name="actionContext">上下文</param>      
 void IHttpController.Execute(ActionContext actionContext)
 {
     var filters = Enumerable.Empty<IFilter>();
     try
     {
         this.CurrentContext = actionContext;
         filters = this.Server.FilterAttributeProvider.GetActionFilters(actionContext.Action);
         this.ExecuteAction(actionContext, filters);
     }
     catch (Exception ex)
     {
         this.ProcessExecutingException(actionContext, filters, ex);
     }
     finally
     {
         this.CurrentContext = null;
     }
 }
Пример #2
0
 /// <summary>
 /// 在执行Api行为前触发       
 /// </summary>
 /// <param name="filterContext">上下文</param>       
 /// <returns></returns>
 void IActionFilter.OnExecuting(ActionContext filterContext)
 {
     this.OnExecuting(filterContext);
 }
Пример #3
0
        /// <summary>
        /// 收到Http请求
        /// </summary>       
        /// <param name="request">请求对象</param>
        /// <param name="response">回复对象</param>
        protected override void OnHttpRequest(HttpRequest request, HttpResponse response)
        {
            var route = request.Url.AbsolutePath;
            var action = this.httpActionList.TryGet(route);
            var requestContext = new RequestContext(request, response);

            if (action == null)
            {
                var exception = new ApiNotExistException(route);
                var exceptionContext = new ExceptionContext(requestContext, exception);
                foreach (var filter in this.GlobalFilter.ExceptionFilters)
                {
                    filter.OnException(exceptionContext);
                    if (exceptionContext.ExceptionHandled == true) break;
                }

                var result = exceptionContext.Result == null ? new ErrorResult { Status = 404, Errors = exception.Message } : exceptionContext.Result;
                result.ExecuteResult(requestContext);
            }
            else
            {
                var controller = this.DependencyResolver.GetService(action.DeclaringService) as HttpController;
                var actionContext = new ActionContext(requestContext, action);

                controller.Server = this;
                ((IHttpController)controller).Execute(actionContext);
            }
        }
Пример #4
0
 /// <summary>
 /// 获取控制器的实例
 /// </summary>
 /// <param name="actionContext">上下文</param>
 /// <returns></returns>
 private IHttpController GetHttpController(ActionContext actionContext)
 {
     try
     {
         var controllerType = actionContext.Action.DeclaringService;
         var controller = this.DependencyResolver.GetService(controllerType) as HttpController;
         controller.Server = this;
         return controller;
     }
     catch (Exception ex)
     {
         var httpException = new HttpException(500, ex.Message);
         this.ProcessHttpException(httpException, actionContext);
         return null;
     }
 }
 public void OnExecuting(ActionContext filterContext)
 {
     Console.WriteLine(message);
     // filterContext.Result = new JsonResult(new { Result = "设置了Result就会中止Action的执行" });
 }
Пример #6
0
        /// <summary>
        /// 调用自身实现的Api行为
        /// 将返回值发送给客户端        
        /// </summary>       
        /// <param name="actionContext">上下文</param>       
        /// <param name="filters">过滤器</param>
        private void ExecuteAction(ActionContext actionContext, IEnumerable<IFilter> filters)
        {
            this.ExecFiltersBeforeAction(filters, actionContext);
            var result = actionContext.Result;

            if (result == null)
            {
                var action = actionContext.Action;
                action.ParameterValues = new object[action.ParameterInfos.Length];

                for (var i = 0; i < action.ParameterValues.Length; i++)
                {
                    var paramter = action.ParameterInfos[i];
                    var pValue = this.Server.ModelBinder.BindModel(actionContext.Request, paramter);
                    action.ParameterValues[i] = pValue;
                }

                result = action.Execute(this, action.ParameterValues) as ActionResult;
                if (result == null) // 直接在方法体里return null
                {
                    throw new Exception("ActionResult不能为null,请使用EmptyResult替代");
                }

                this.ExecFiltersAfterAction(filters, actionContext);
                if (actionContext.Result != null)
                {
                    result = actionContext.Result;
                }
            }

            result.ExecuteResult(actionContext);
        }
Пример #7
0
        /// <summary>
        /// 在Api行为后执行过滤器
        /// </summary>       
        /// <param name="actionFilters">Api行为过滤器</param>
        /// <param name="actionContext">上下文</param>       
        private void ExecFiltersAfterAction(IEnumerable<IFilter> actionFilters, ActionContext actionContext)
        {
            // 全局过滤器
            foreach (var globalFilter in this.Server.GlobalFilter.ActionFilters)
            {
                globalFilter.OnExecuted(actionContext);
                if (actionContext.Result != null) return;
            }

            // 自身过滤器
            ((IActionFilter)this).OnExecuted(actionContext);
            if (actionContext.Result != null) return;

            // 特性过滤器
            foreach (var filter in actionFilters)
            {
                var actionFilter = filter as IActionFilter;
                if (actionFilter != null)
                {
                    actionFilter.OnExecuted(actionContext);
                    if (actionContext.Result != null) return;
                }
            }
        }
Пример #8
0
 /// <summary>
 /// 授权时触发       
 /// </summary>
 /// <param name="filterContext">上下文</param>       
 /// <returns></returns>
 protected virtual void OnAuthorization(ActionContext filterContext)
 {
 }
Пример #9
0
        /// <summary>
        /// 在Api行为前 执行过滤器
        /// </summary>       
        /// <param name="filters">Api行为过滤器</param>
        /// <param name="actionContext">上下文</param>   
        private void ExecFiltersBeforeAction(IEnumerable<IFilter> filters, ActionContext actionContext)
        {
            var totalFilters = this.Server
                .GlobalFilters
                .Cast<IFilter>()
                .Concat(new[] { this })
                .Concat(filters);

            foreach (var filter in totalFilters)
            {
                filter.OnExecuting(actionContext);
                if (actionContext.Result != null) break;
            }
        }
Пример #10
0
 /// <summary>
 /// 在Api行为后执行过滤器
 /// </summary>       
 /// <param name="filters">Api行为过滤器</param>
 /// <param name="actionContext">上下文</param>       
 private void ExecFiltersAfterAction(IEnumerable<IFilter> filters, ActionContext actionContext)
 {
     var totalFilters = this.GetTotalFilters(filters);
     foreach (var filter in totalFilters)
     {
         filter.OnExecuted(actionContext);
         if (actionContext.Result != null) break;
     }
 }
Пример #11
0
 /// <summary>
 /// 异常上下文
 /// </summary>
 /// <param name="actionContext">api行为上下文</param>
 /// <param name="exception">异常</param>
 public ExceptionContext(ActionContext actionContext, Exception exception)
     : base(actionContext.Request, actionContext.Response)
 {
     this.Exception = exception;
 }
Пример #12
0
        /// <summary>
        /// 处理http请求
        /// </summary>
        /// <param name="requestContext">请求上下文</param>
        private void ProcessRequest(RequestContext requestContext)
        {
            var route = requestContext.Request.Url.AbsolutePath;
            var action = this.httpActionList.TryGet(requestContext.Request);

            if (action == null)
            {
                var exception = new ApiNotExistException(route);
                var exceptionContext = new ExceptionContext(requestContext, exception);
                foreach (var filter in this.GlobalFilter.ExceptionFilters)
                {
                    filter.OnException(exceptionContext);
                    if (exceptionContext.ExceptionHandled == true) break;
                }

                var result = exceptionContext.Result != null ? exceptionContext.Result : new ErrorResult
                {
                    Status = 404,
                    Errors = exception.Message
                };
                result.ExecuteResult(requestContext);
            }
            else
            {
                var controller = this.DependencyResolver.GetService(action.DeclaringService) as HttpController;
                var actionContext = new ActionContext(requestContext, action);

                controller.Server = this;
                ((IHttpController)controller).Execute(actionContext);

                // 释放资源
                this.DependencyResolver.TerminateService(controller);
            }
        }
Пример #13
0
        /// <summary>
        /// 执行httpAction
        /// </summary>
        /// <param name="action">httpAction</param>
        /// <param name="requestContext">上下文</param>      
        private void ExecuteHttpAction(HttpAction action, RequestContext requestContext)
        {
            var controller = this.DependencyResolver.GetService(action.DeclaringService) as HttpController;
            var actionContext = new ActionContext(requestContext, action);

            controller.Server = this;
            ((IHttpController)controller).Execute(actionContext);

            // 释放资源
            this.DependencyResolver.TerminateService(controller);
        }
Пример #14
0
        /// <summary>
        /// 处理Api行为执行过程中产生的异常
        /// </summary>
        /// <param name="actionContext">上下文</param>
        /// <param name="actionfilters">过滤器</param>
        /// <param name="exception">异常项</param>
        private void ProcessExecutingException(ActionContext actionContext, IEnumerable<IFilter> actionfilters, Exception exception)
        {
            var exceptionContext = new ExceptionContext(actionContext, new ApiExecuteException(exception));
            this.ExecAllExceptionFilters(actionfilters, exceptionContext);

            var result = exceptionContext.Result;
            if (result == null)
            {
                result = new ErrorResult { Status = 500, Errors = exception.Message };
            }
            result.ExecuteResult(actionContext);
        }
Пример #15
0
 /// <summary>
 /// 授权时触发       
 /// </summary>
 /// <param name="filterContext">上下文</param>       
 /// <returns></returns>
 void IAuthorizationFilter.OnAuthorization(ActionContext filterContext)
 {
     this.OnAuthorization(filterContext);
 }
Пример #16
0
        /// <summary>
        /// 调用自身实现的Api行为
        /// 将返回值发送给客户端        
        /// </summary>       
        /// <param name="actionContext">上下文</param>       
        /// <param name="filters">过滤器</param>
        /// <returns>如果输出Api的返回结果就返回true</returns>
        private bool ExecuteAction(ActionContext actionContext, IEnumerable<IFilter> filters)
        {
            // Api参数准备
            var parameters = this.GetAndUpdateParameterValues(actionContext);

            // Action执行前
            this.ExecFiltersBeforeAction(filters, actionContext);
            if (actionContext.Result != null)
            {
                actionContext.Result.ExecuteResult(actionContext);
                return false;
            }

            // 执行Action
            var apiResult = actionContext.Action.Execute(this, parameters) as ActionResult;
            if (apiResult == null) // 直接在方法体里return null
            {
                throw new Exception("ActionResult不能为null,请使用EmptyResult替代");
            }

            // Action执行后
            this.ExecFiltersAfterAction(filters, actionContext);
            if (actionContext.Result != null)
            {
                actionContext.Result.ExecuteResult(actionContext);
                return false;
            }

            apiResult.ExecuteResult(actionContext);
            return true;
        }
Пример #17
0
        /// <summary>
        /// 执行Api行为
        /// </summary>   
        /// <param name="actionContext">上下文</param>      
        void IHttpController.Execute(ActionContext actionContext)
        {
            this.CurrentContext = actionContext;
            var filters = this.Server.FilterAttributeProvider.GetActionFilters(actionContext.Action);

            try
            {
                this.ExecuteAction(actionContext, filters);
            }
            catch (AggregateException exception)
            {
                foreach (var inner in exception.InnerExceptions)
                {
                    this.ProcessExecutingException(actionContext, filters, inner);
                }
            }
            catch (Exception exception)
            {
                this.ProcessExecutingException(actionContext, filters, exception);
            }
            finally
            {
                this.CurrentContext = null;
            }
        }
Пример #18
0
        /// <summary>
        /// 获取和更新Http Api的参数值
        /// </summary>
        /// <param name="actionContext"></param>
        /// <returns></returns>
        private object[] GetAndUpdateParameterValues(ActionContext actionContext)
        {
            var action = actionContext.Action;
            action.ParameterValues = new object[action.ParameterInfos.Length];

            for (var i = 0; i < action.ParameterValues.Length; i++)
            {
                var paramter = action.ParameterInfos[i];
                var pValue = this.Server.ModelBinder.BindModel(actionContext.Request, paramter);
                action.ParameterValues[i] = pValue;
            }
            return action.ParameterValues;
        }
Пример #19
0
 /// <summary>
 /// 在执行Api行为前触发       
 /// </summary>
 /// <param name="filterContext">上下文</param>       
 /// <returns></returns>
 protected virtual void OnExecuting(ActionContext filterContext)
 {
 }
Пример #20
0
 protected override void OnExecuting(ActionContext filterContext)
 {
     Console.WriteLine(message);            
 }
Пример #21
0
        /// <summary>
        /// 在Api行为前 执行过滤器
        /// </summary>       
        /// <param name="actionFilters">Api行为过滤器</param>
        /// <param name="actionContext">上下文</param>   
        private void ExecFiltersBeforeAction(IEnumerable<IFilter> actionFilters, ActionContext actionContext)
        {
            // OnAuthorization
            foreach (var globalFilter in this.Server.GlobalFilter.AuthorizationFilters)
            {
                globalFilter.OnAuthorization(actionContext);
                if (actionContext.Result != null) return;
            }

            ((IAuthorizationFilter)this).OnAuthorization(actionContext);
            if (actionContext.Result != null) return;

            foreach (var filter in actionFilters)
            {
                var authorizationFilter = filter as IAuthorizationFilter;
                if (authorizationFilter != null)
                {
                    authorizationFilter.OnAuthorization(actionContext);
                    if (actionContext.Result != null) return;
                }
            }

            // OnExecuting
            foreach (var globalFilter in this.Server.GlobalFilter.ActionFilters)
            {
                globalFilter.OnExecuting(actionContext);
                if (actionContext.Result != null) return;
            }

            ((IActionFilter)this).OnExecuting(actionContext);
            if (actionContext.Result != null) return;

            foreach (var filter in actionFilters)
            {
                var actionFilter = filter as IActionFilter;
                if (actionFilter != null)
                {
                    actionFilter.OnExecuting(actionContext);
                    if (actionContext.Result != null) return;
                }
            }
        }
Пример #22
0
        /// <summary>
        /// 执行httpAction
        /// </summary>
        /// <param name="action">httpAction</param>
        /// <param name="context">上下文</param>
        /// <param name="requestContext">请求上下文</param>      
        private void ExecuteHttpAction(HttpAction action, IContenxt context, RequestContext requestContext)
        {
            var actionContext = new ActionContext(requestContext, action, context);
            var controller = GetHttpController(actionContext);

            if (controller != null)
            {
                controller.Execute(actionContext);
                this.DependencyResolver.TerminateService(controller);
            }
        }
Пример #23
0
        /// <summary>
        /// 处理Api行为执行过程中产生的异常
        /// </summary>
        /// <param name="actionContext">上下文</param>
        /// <param name="actionfilters">过滤器</param>
        /// <param name="exception">异常项</param>
        private void ProcessExecutingException(ActionContext actionContext, IEnumerable<IFilter> actionfilters, Exception exception)
        {
            var exceptionContext = new ExceptionContext(actionContext, new ApiExecuteException(actionContext, exception));
            this.ExecExceptionFilters(actionfilters, exceptionContext);

            if (exceptionContext.ExceptionHandled == false)
            {
                throw exception;
            }

            var result = exceptionContext.Result == null ? new ErrorResult { Status = 500, Errors = exception.Message } : exceptionContext.Result;
            result.ExecuteResult(actionContext);
        }
 public void OnExecuted(ActionContext filterContext)
 {
 }