Ejemplo n.º 1
0
        /// <summary>
        /// 根据服务转发
        /// </summary>
        /// <param name="input"></param>
        /// <param name="type">表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型</param>
        /// <returns></returns>
        public static ActionResult Transmit(Dictionary <string, string> input, Routing.RoutInfo routInfo)
        {
            BaseModule module = null;

            try
            {
                Interlocked.Increment(ref engineCounter);
                #region Cache
                string key = string.Empty;
                if (routInfo.CacheMiddleware.Count > 0)
                {
                    key = GetDicStrHashCode(input);
                    if (TryGetCache(routInfo, key, out ActionResult rltCache))
                    {
                        return(rltCache);
                    }
                }
                #endregion
                List <object> lo = new List <object>()
                {
                    input
                };
                module = Loader.IocLoader.Resolve <BaseModule>(routInfo.RoutModuleType);
                var init = module.Init(input);
                if (!init)
                {
                    return(new ActionResult()
                    {
                        Status = false,
                        Msg = "Init拦截!"
                    });
                }
                if (routInfo.RoutMethod == null)
                {
                    return(new ActionResult()
                    {
                        Status = false,
                        OutputData = null,
                        Msg = $"在【{input[Eng.NAMESPACE]}】中找不到【{input[Eng.CLASS]}.{input[Eng.METHOD]}】!"
                    });
                }
                #region Authorization
                for (int i = 0; i < routInfo.AuthorizationFilters.Count; i++)
                {
                    routInfo.AuthorizationFilters[i].OnAuthorization(module);
                    if (!module.Authorized)
                    {
                        return(module.ActionResult == null ? new ActionResult()
                        {
                            Status = false,
                            OutputData = 401,
                            Msg = "401,Unauthrized"
                        } : module.ActionResult
                               );
                    }
                }
                #endregion
                for (int i = 0; i < routInfo.ActionFilters.Count; i++)
                {
                    routInfo.ActionFilters[i].OnActionExecuting(module);
                }
                #region 调用业务方法
                object rltCustomize = null;
                if (routInfo.ReturnTypeIsTask)
                {
                    var rlt = (routInfo.RoutMethod.Invoke(module, DicToParameters(routInfo.RoutMethod, input).ToArray()) as Task);
                    rltCustomize = routInfo.RoutMethod.ReturnType.GetProperty("Result").GetValue(rlt, null);
                }
                else
                {
                    rltCustomize = routInfo.RoutMethod.Invoke(module, DicToParameters(routInfo.RoutMethod, input).ToArray());
                }

                if (routInfo.ReturnTypeIsIActionResult && rltCustomize != null)
                {
                    module.ActionResult = rltCustomize as ActionResult;
                }
                else
                {
                    module.ActionResult = new ActionResult(true, rltCustomize);
                }
                #endregion
                for (int i = (routInfo.ActionFilters.Count - 1); i >= 0; i--)
                {
                    routInfo.ActionFilters[i].OnActionExecuted(module);
                }
                if (routInfo.CacheMiddleware.Count > 0)
                {
                    AddCache(routInfo, key, module.ActionResult);
                }
                return(module.ActionResult);
            }
            catch (Exception ex)
            {
                if (routInfo.RoutMethod != null)
                {
                    if (module.ActionResult != null)
                    {
                        module.ActionResult.Status = false;
                        module.ActionResult.Msg    = ex.InnerException?.Message ?? ex.Message;
                    }
                    foreach (var ef in routInfo.ExceptionFilters)
                    {
                        ef.OnException(ex, module);
                    }
                }
#if DEBUG
                //记录日志
                Log.Log.Error(ex, routInfo.RoutModuleType);
#endif
                return(module.ActionResult ?? new ActionResult()
                {
                    Status = false,
                    OutputData = null,
                    Msg = ex.InnerException?.Message ?? ex.Message
                });
            }
            finally
            {
                Interlocked.Decrement(ref engineCounter);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 根据服务转发
        /// </summary>
        /// <param name="input"></param>
        /// <param name="type">表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型</param>
        /// <returns></returns>
        public static ActionResult Transmit(Dictionary <string, string> input, Routing.RoutInfo routInfo)
        {
            BaseModule module = null;

            try
            {
                #region Cache
                string key = string.Empty;
                if (routInfo.CacheMiddleware.Count > 0)
                {
                    key = GetDicStrHashCode(input);
                    if (TryGetCache(routInfo, key, out ActionResult rltCache))
                    {
                        return(rltCache);
                    }
                }
                #endregion
                List <object> lo = new List <object>()
                {
                    input
                };
                module = Loader.IocLoader.Resolve <BaseModule>(routInfo.RoutModuleType); //创建实例(无参构造器)
                var init = module.Init(input);
                if (!init)
                {
                    return(new ActionResult()
                    {
                        Status = false,
                        Msg = "Init拦截!"
                    });
                }
                if (routInfo.RoutMethod == null)
                {
                    return(new ActionResult()
                    {
                        Status = false,
                        OutputData = null,
                        Msg = $"在【{input[Eng.NAMESPACE]}】中找不到【{input[Eng.CLASS]}.{input[Eng.METHOD]}】!"
                    });
                }
                #region Authorization
                for (int i = 0; i < routInfo.AuthorizationFilters.Count; i++)
                {
                    routInfo.AuthorizationFilters[i].OnAuthorization(module);
                    if (!module.Authorized)
                    {
                        return(new ActionResult()
                        {
                            Status = false,
                            OutputData = 401,
                            Msg = "401,Unauthrized"
                        });
                    }
                }
                #endregion
                for (int i = 0; i < routInfo.ActionFilters.Count; i++)
                {
                    routInfo.ActionFilters[i].OnActionExecuting(module);
                }
                var rltCustomize = routInfo.RoutMethod.Invoke(module, DicToParameters(routInfo.RoutMethod, input).ToArray());
                if (rltCustomize != null && typeof(IActionResult).IsAssignableFrom(rltCustomize.GetType()))
                {
                    module.ActionResult = rltCustomize as ActionResult;
                }
                else
                {
                    module.ActionResult = new ActionResult(true, rltCustomize);
                }
                for (int i = (routInfo.ActionFilters.Count - 1); i >= 0; i--)
                {
                    routInfo.ActionFilters[i].OnActionExecuted(module);
                }
                if (routInfo.CacheMiddleware.Count > 0)
                {
                    AddCache(routInfo, key, module.ActionResult);
                }
                return(module.ActionResult);
            }
            catch (Exception ex)
            {
                if (routInfo.RoutMethod != null)
                {
                    foreach (var ef in routInfo.ExceptionFilters)
                    {
                        ef.OnException(ex, module);
                    }
                }
                //记录日志
                Log.Log.Error(ex, routInfo.RoutModuleType);
                return(new ActionResult()
                {
                    Status = false,
                    OutputData = null,
                    Msg = ex.InnerException?.Message ?? ex.Message
                });
            }
        }