/// <summary>
 /// Api行为上下文
 /// </summary>
 /// <param name="requestContext">请求上下文</param>
 /// <param name="action">Api行为</param>
 /// <param name="context">上下文</param>
 public ActionContext(RequestContext requestContext, HttpAction action, IContenxt context)
     : base(requestContext.Request, requestContext.Response)
 {
     this.Action = action;
     this.Session = context.Session;
     this.AllSessions = context.AllSessions;
 }
 /// <summary>
 /// Api行为上下文
 /// </summary>
 /// <param name="requestContext">请求上下文</param>
 /// <param name="action">Api行为</param>
 /// <param name="context">上下文</param>
 public ActionContext(RequestContext requestContext, HttpAction action, IContenxt context)
     : base(requestContext.Request, requestContext.Response)
 {
     this.Action      = action;
     this.Session     = context.Session;
     this.AllSessions = context.AllSessions;
 }
 /// <summary>
 /// 获取服务类型的Api行为
 /// </summary>
 /// <param name="controller">服务类型</param>
 /// <exception cref="ArgumentException"></exception>
 /// <returns></returns>
 private static IEnumerable <HttpAction> GetControllerHttpActions(Type controller)
 {
     return(controller
            .GetMethods()
            .Where(item => HttpAction.IsSupport(item))
            .Select(method => new HttpAction(method, controller)));
 }
        /// <summary>
        /// 获取Api行为的特性过滤器     
        /// </summary>
        /// <param name="action">Api行为</param>
        /// <returns></returns>
        private static IEnumerable<IFilter> GetActionFiltersNoCached(HttpAction action)
        {
            var methodAttributes = action.GetMethodFilterAttributes();

            var classAttributes = action.GetClassFilterAttributes()
                .Where(filter => filter.AllowMultiple || methodAttributes.Any(mFilter => mFilter.TypeId == filter.TypeId) == false);

            var methodFilters = methodAttributes.Select(fiter => new
            {
                Filter = fiter,
                Level = (fiter is IAuthorizationFilter) ? FilterLevels.Authorization : FilterLevels.Method
            });

            var classFilters = classAttributes.Select(fiter => new
            {
                Filter = fiter,
                Level = (fiter is IAuthorizationFilter) ? FilterLevels.Authorization : FilterLevels.Class
            });

            var filters = classFilters.Concat(methodFilters)
                .OrderBy(filter => filter.Level)
                .ThenBy(filter => filter.Filter.Order)
                .Select(filter => filter.Filter);

            return filters;
        }
 /// <summary>
 /// 获取服务类型的Api行为
 /// </summary>
 /// <param name="controller">服务类型</param>
 /// <returns></returns>
 private IEnumerable <HttpAction> GetHttpActions(Type controller)
 {
     return(controller
            .GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
            .Where(item => HttpAction.IsHttpAction(item))
            .Select(method => new HttpAction(method, controller)));
 }
        /// <summary>
        /// 获取Api行为的特性过滤器
        /// </summary>
        /// <param name="action">Api行为</param>
        /// <returns></returns>
        private static IEnumerable <IFilter> GetActionFiltersNoCached(HttpAction action)
        {
            var methodAttributes = action.GetMethodFilterAttributes();

            var classAttributes = action.GetClassFilterAttributes()
                                  .Where(filter => filter.AllowMultiple || methodAttributes.Any(mFilter => mFilter.TypeId == filter.TypeId) == false);

            var methodFilters = methodAttributes.Select(fiter => new
            {
                Filter = fiter,
                Level  = (fiter is IAuthorizationFilter) ? FilterLevels.Authorization : FilterLevels.Method
            });

            var classFilters = classAttributes.Select(fiter => new
            {
                Filter = fiter,
                Level  = (fiter is IAuthorizationFilter) ? FilterLevels.Authorization : FilterLevels.Class
            });

            var filters = classFilters.Concat(methodFilters)
                          .OrderBy(filter => filter.Level)
                          .ThenBy(filter => filter.Filter.Order)
                          .Select(filter => filter.Filter);

            return(filters);
        }
Exemple #7
0
 /// <summary>
 /// Http的Api行为
 /// </summary>
 /// <param name="method">方法信息</param>
 /// <param name="declaringType">声明的类型</param>
 /// <exception cref="ArgumentException"></exception>
 public HttpAction(MethodInfo method, Type declaringType)
     : base(method)
 {
     this.DeclaringService = declaringType;
     this.ControllerName   = Regex.Replace(declaringType.Name, @"Controller$", string.Empty, RegexOptions.IgnoreCase);
     this.AllowMethod      = HttpAction.GetAllowMethod(method);
     this.Route            = this.GetRouteAttribute();
 }
        /// <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);
            }
        }
        /// <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);
        }
        /// <summary>
        /// 添加Api行为
        /// </summary>
        /// <param name="httpAction">Api行为</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public void Add(HttpAction httpAction)
        {
            if (httpAction == null)
            {
                throw new ArgumentNullException("apiAction");
            }

            if (this.dictionary.ContainsKey(httpAction.Route))
            {
                throw new ArgumentException(string.Format("Http行为:{0}存在冲突的路由规则", httpAction.Route));
            }
            this.dictionary.Add(httpAction.Route, httpAction);
        }
        /// <summary>
        /// 添加Api行为
        /// </summary>
        /// <param name="httpAction">Api行为</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public void Add(HttpAction httpAction)
        {
            if (httpAction == null)
            {
                throw new ArgumentNullException("apiAction");
            }

            var key = httpAction.GetHashCode();
            if (this.dictionary.ContainsKey(key) == true)
            {
                throw new ArgumentException(string.Format("Http行为:{0}存在冲突的路由规则", httpAction.Route));
            }
            this.dictionary.Add(key, httpAction);
        }
        /// <summary>
        /// 执行httpAction
        /// </summary>
        /// <param name="action">httpAction</param>
        /// <param name="context">上下文</param>
        /// <param name="requestContext">请求上下文</param>
        /// <returns></returns>
        private async Task ExecuteHttpActionAsync(HttpAction action, IContenxt context, RequestContext requestContext)
        {
            try
            {
                var actionContext = new ActionContext(requestContext, action, context);
                var controller    = this.GetHttpController(actionContext);
                await controller.ExecuteAsync(actionContext);

                this.DependencyResolver.TerminateService(controller);
            }
            catch (Exception ex)
            {
                this.OnException(context.Session, ex);
            }
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="httpAction">http行为</param>
        protected override void Init(HttpAction httpAction)
        {
            this.fixRule = Regex.Replace(this.Rule, @"\{controller\}", httpAction.ControllerName, RegexOptions.IgnoreCase);
            this.fixRule = Regex.Replace(this.fixRule, @"\{action\}", httpAction.ApiName, RegexOptions.IgnoreCase);

            var glob    = Regex.Escape(this.fixRule).Replace(@"\*", ".*").Replace(@"\?", ".").Replace(@"\{", "{");
            var pattern = Regex.Replace(glob, @"\{\w+\}", (m) =>
            {
                var key = m.Value.TrimStart('{').TrimEnd('}');
                this.keys.Add(key);
                return(string.Format(@"(?<{0}>\w+)", key));
            });

            this.ruleRegex = new Regex("^" + pattern + "/?$", RegexOptions.IgnoreCase);
        }
        /// <summary>
        /// 添加Api行为
        /// </summary>
        /// <param name="httpAction">Api行为</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public void Add(HttpAction httpAction)
        {
            if (httpAction == null)
            {
                throw new ArgumentNullException("apiAction");
            }

            var key = httpAction.GetHashCode();

            if (this.dictionary.ContainsKey(key) == true)
            {
                throw new ArgumentException(string.Format("Http行为:{0}存在冲突的路由规则", httpAction.Route));
            }
            this.dictionary.Add(key, httpAction);
        }
        /// <summary>
        /// 绑定httpAction
        /// </summary>
        /// <param name="httpAction">http行为</param>
        /// <returns></returns>
        internal RouteAttribute BindHttpAction(HttpAction httpAction)
        {
            this.Rule = Regex.Replace(this.Rule, @"\{controller\}", httpAction.ControllerName, RegexOptions.IgnoreCase);
            this.Rule = Regex.Replace(this.Rule, @"\{action\}", httpAction.ApiName, RegexOptions.IgnoreCase);

            var glob    = Regex.Escape(this.Rule).Replace(@"\*", ".*").Replace(@"\?", ".").Replace(@"\{", "{");
            var pattern = Regex.Replace(glob, @"\{\w+\}", (m) =>
            {
                var token = m.Value.TrimStart('{').TrimEnd('}');
                this.RouteDatas.Set(token, null);
                return(string.Format(@"(?<{0}>\w+)", token));
            });

            this.ruleRegex = new Regex(pattern, RegexOptions.IgnoreCase);
            return(this);
        }
Exemple #16
0
        /// <summary>
        /// 匹配请求,如果成功则返回克隆
        /// </summary>
        /// <param name="action">http行为</param>
        /// <param name="request">请求</param>
        /// <returns></returns>
        private HttpAction MatchAndClone(HttpAction action, HttpRequest request)
        {
            var method = request.HttpMethod & action.AllowMethod;

            if (request.HttpMethod != method)
            {
                return(null);
            }
            var routeData = action.Route.MatchURL(request.Url);

            if (routeData == null)
            {
                return(null);
            }

            var clone = ((ICloneable <HttpAction>)action).CloneConstructor();

            foreach (var kv in routeData)
            {
                clone.RouteDatas.Set(kv.Key, kv.Value);
            }
            return(clone);
        }
 /// <summary>
 /// 获取Api行为的特性过滤器
 /// </summary>
 /// <param name="apiAction">Api行为</param>
 /// <returns></returns>
 public virtual IEnumerable <IFilter> GetActionFilters(HttpAction apiAction)
 {
     return(this.filterCached.GetOrAdd(apiAction, action => GetActionFiltersNoCached(action)));
 }
 /// <summary>
 /// 获取Api行为的特性过滤器     
 /// </summary>
 /// <param name="apiAction">Api行为</param>
 /// <returns></returns>
 public virtual IEnumerable<IFilter> GetActionFilters(HttpAction apiAction)
 {
     return this.filterCached.GetOrAdd(apiAction, action => GetActionFiltersNoCached(action));
 }
        /// <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);
            }
        }
Exemple #20
0
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="httpAction">http行为</param>
 protected abstract void Init(HttpAction httpAction);
Exemple #21
0
 /// <summary>
 /// 初始化
 /// </summary>
 /// <param name="httpAction">http行为</param>
 /// <returns></returns>
 internal RouteBaseAttribute InitWith(HttpAction httpAction)
 {
     this.Init(httpAction);
     return(this);
 }
 /// <summary>
 /// Api行为上下文
 /// </summary>
 /// <param name="context">请求上下文</param>
 /// <param name="action">Api行为</param>
 public ActionContext(RequestContext context, HttpAction action)
     : base(context.Request, context.Response)
 {
     this.Action = action;
 }
Exemple #23
0
 /// <summary>
 /// Api行为上下文
 /// </summary>
 /// <param name="context">请求上下文</param>
 /// <param name="action">Api行为</param>
 public ActionContext(RequestContext context, HttpAction action)
     : base(context.Request, context.Response)
 {
     this.Action = action;
 }
        /// <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);
        }