Esempio n. 1
0
 /// <summary>
 /// 执行Api行为
 /// </summary>   
 /// <param name="actionContext">上下文</param>      
 void IFastApiService.Execute(ActionContext actionContext)
 {
     var filters = Enumerable.Empty<IFilter>();
     try
     {
         this.CurrentContext = actionContext;
         filters = this.Server.FilterAttributeProvider.GetActionFilters(actionContext.Action);
         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;
     }
 }
Esempio n. 2
0
 /// <summary>
 /// 检查是否已登录
 /// </summary>
 /// <param name="filterContext"></param>
 public void OnAuthorization(ActionContext filterContext)
 {
     var logined = filterContext.Session.TagData.TryGet<bool>("Logined");
     if (logined == false && bool.Parse(ConfigurationManager.AppSettings["NeedLogin"]) == true)
     {
         throw new Exception("请登录系统后再操作 ..");
     }
 }
 protected override void OnExecuting(ActionContext filterContext)
 {
     var valid = filterContext.Session.TagData.TryGet<bool>("Logined");
     if (valid == false)
     {
         // filterContext.Session.Close();
         filterContext.Result = "未登录就尝试请求其它服务";
     }
 }
Esempio n. 4
0
        public void OnAuthorization(NetworkSocket.Fast.ActionContext filterContext)
        {
            var valid = filterContext.Session.TagData.TryGet <bool>("Logined");

            if (valid == false)
            {
                // 直接关闭客户端的连接
                // filterContext.Session.Close();

                // 以异常方式提示客户端
                throw new Exception("未登录就尝试请求其它服务");
            }
        }
Esempio n. 5
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;
     }
 }
Esempio n. 6
0
        /// <summary>
        /// 获取FastApiService实例
        /// </summary>
        /// <param name="actionContext">Api行为上下文</param>
        /// <returns></returns>
        private IFastApiService GetFastApiService(ActionContext actionContext)
        {
            try
            {
                var serviceType = actionContext.Action.DeclaringService;
                var instance = this.DependencyResolver.GetService(serviceType);
                return instance as IFastApiService;
            }
            catch (Exception ex)
            {
                var exception = new ResolveException(actionContext.Action.DeclaringService, ex);
                var exceptionContext = new ExceptionContext(actionContext, exception);

                Common.SendRemoteException(actionContext.Session.UnWrap(), exceptionContext);
                this.ExecGlobalExceptionFilters(exceptionContext);
                return null;
            }
        }
Esempio n. 7
0
        /// <summary>
        /// 处理正常的数据请求
        /// </summary>
        /// <param name="requestContext">请求上下文</param>       
        private void ProcessRequest(RequestContext requestContext)
        {
            if (requestContext.Packet.IsFromClient == false)
            {
                Common.SetApiActionTaskResult(requestContext, this.TaskSetActionTable, this.Serializer);
                return;
            }

            var action = this.GetApiAction(requestContext);
            if (action == null)
            {
                return;
            }

            var actionContext = new ActionContext(requestContext, action);
            var fastApiService = this.GetFastApiService(actionContext);
            if (fastApiService == null)
            {
                return;
            }

            // 执行Api行为           
            fastApiService.Execute(actionContext);
            this.DependencyResolver.TerminateService(fastApiService);
        }
 /// <summary>
 /// 在执行Api行为后触发
 /// </summary>
 /// <param name="filterContext">上下文</param>      
 protected virtual void OnExecuted(ActionContext filterContext)
 {
 }
 public void OnExecuted(ActionContext filterContext)
 {
 }
Esempio n. 10
0
        /// <summary>
        /// 调用自身实现的Api行为
        /// 将返回值发送给客户端        
        /// </summary>       
        /// <param name="actionContext">上下文</param>       
        /// <param name="filters">过滤器</param>
        /// <returns>当正常执行输出Api的结果时返回true</returns>
        private bool ExecuteAction(ActionContext actionContext, IEnumerable<IFilter> filters)
        {
            var action = actionContext.Action;
            var packet = actionContext.Packet;
            var session = actionContext.Session;
            var serializer = this.Server.Serializer;
            var parameters = Common.GetAndUpdateParameterValues(serializer, actionContext);

            // Api执行前
            this.ExecFiltersBeforeAction(filters, actionContext);
            if (actionContext.Result != null)
            {
                var exceptionContext = new ExceptionContext(actionContext, actionContext.Result);
                Common.SendRemoteException(actionContext.Session.UnWrap(), exceptionContext);
                return false;
            }

            // 执行Api            
            var apiResult = action.Execute(this, parameters);

            // Api执行后
            this.ExecFiltersAfterAction(filters, actionContext);
            if (actionContext.Result != null)
            {
                var exceptionContext = new ExceptionContext(actionContext, actionContext.Result);
                Common.SendRemoteException(actionContext.Session.UnWrap(), exceptionContext);
                return false;
            }

            // 返回数据
            if (action.IsVoidReturn == false && session.IsConnected)
            {
                packet.Body = serializer.Serialize(apiResult);
                session.UnWrap().Send(packet.ToByteRange());
            }
            return true;
        }
Esempio n. 11
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.CurrentContext.Session.GlobalFilter.AuthorizationFilters)
            {
                globalFilter.OnAuthorization(actionContext);
            }
            ((IAuthorizationFilter)this).OnAuthorization(actionContext);
            foreach (var filter in actionFilters)
            {
                var authorizationFilter = filter as IAuthorizationFilter;
                if (authorizationFilter != null)
                {
                    authorizationFilter.OnAuthorization(actionContext);
                }
            }

            // OnExecuting
            foreach (var globalFilter in this.CurrentContext.Session.GlobalFilter.ActionFilters)
            {
                globalFilter.OnExecuting(actionContext);
            }

            ((IActionFilter)this).OnExecuting(actionContext);

            foreach (var filter in actionFilters)
            {
                var actionFilter = filter as IActionFilter;
                if (actionFilter != null)
                {
                    actionFilter.OnExecuting(actionContext);
                }
            }
        }
Esempio n. 12
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.CurrentContext.Session.GlobalFilter.ActionFilters)
            {
                globalFilter.OnExecuted(actionContext);
            }

            // 自身过滤器
            ((IActionFilter)this).OnExecuted(actionContext);

            // 特性过滤器
            foreach (var filter in actionFilters)
            {
                var actionFilter = filter as IActionFilter;
                if (actionFilter != null)
                {
                    actionFilter.OnExecuted(actionContext);
                }
            }
        }
Esempio n. 13
0
 /// <summary>
 /// 授权时触发       
 /// </summary>
 /// <param name="filterContext">上下文</param>       
 /// <returns></returns>
 protected virtual void OnAuthorization(ActionContext filterContext)
 {
 }
Esempio n. 14
0
 /// <summary>
 /// 授权时触发       
 /// </summary>
 /// <param name="filterContext">上下文</param>       
 /// <returns></returns>
 void IAuthorizationFilter.OnAuthorization(ActionContext filterContext)
 {
     this.OnAuthorization(filterContext);
 }
Esempio n. 15
0
 /// <summary>
 /// 在执行Api行为前触发       
 /// </summary>
 /// <param name="filterContext">上下文</param>       
 /// <returns></returns>
 void IActionFilter.OnExecuting(ActionContext filterContext)
 {
     this.OnExecuting(filterContext);
 }
Esempio n. 16
0
 /// <summary>
 /// 处理Api行为执行过程中产生的异常
 /// </summary>
 /// <param name="actionContext">上下文</param>
 /// <param name="filters">过滤器</param>
 /// <param name="exception">异常项</param>
 private void ProcessExecutingException(ActionContext actionContext, IEnumerable<IFilter> filters, Exception exception)
 {
     var exceptionContext = new ExceptionContext(actionContext, new ApiExecuteException(exception));
     Common.SendRemoteException(actionContext.Session.UnWrap(), exceptionContext);
     this.ExecAllExceptionFilters(filters, exceptionContext);
 }
Esempio n. 17
0
        /// <summary>
        /// 调用自身实现的Api行为
        /// 将返回值发送给客户端        
        /// </summary>       
        /// <param name="actionContext">上下文</param>       
        /// <param name="filters">过滤器</param>
        private void ExecuteAction(ActionContext actionContext, IEnumerable<IFilter> filters)
        {
            var action = actionContext.Action;
            var packet = actionContext.Packet;
            var session = actionContext.Session;
            var serializer = session.Serializer;
            action.ParameterValues = packet.GetBodyParameters(serializer, action.ParameterTypes);

            this.ExecFiltersBeforeAction(filters, actionContext);
            var returnValue = action.Execute(this, action.ParameterValues);
            this.ExecFiltersAfterAction(filters, actionContext);

            // 返回数据
            if (action.IsVoidReturn == false && session.IsConnected)
            {
                packet.Body = serializer.Serialize(returnValue);
                session.Send(packet.ToByteRange());
            }
        }
 protected override void OnExecuting(ActionContext filterContext)
 {
     var log = string.Format("Time:{0} Client:{1} Action:{2} Message:{3}", DateTime.Now.ToString("mm:ss"), filterContext.Session, filterContext.Action, this.message);
     this.Loger.Write(log);
 }
Esempio n. 19
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));
            FastTcpCommon.SetRemoteException(actionContext.Session, exceptionContext);
            this.ExecExceptionFilters(actionfilters, exceptionContext);

            if (exceptionContext.ExceptionHandled == false)
            {
                throw exception;
            }
        }
Esempio n. 20
0
 /// <summary>
 /// 异常上下文
 /// </summary>
 /// <param name="context">请求上下文</param>
 /// <param name="exception">异常</param>
 public ExceptionContext(ActionContext context, Exception exception)
     : base(context.Session, context.Packet, context.AllSessions)
 {
     this.Exception = exception;
 }
 /// <summary>
 /// Api执行异常
 /// </summary>
 /// <param name="actionContext">Api行为上下文</param>
 /// <param name="innerException">内部异常</param>
 public ApiExecuteException(ActionContext actionContext, Exception innerException)
     : base(innerException.Message, innerException)
 {
     this.ActionContext = actionContext;
 }