Exemple #1
0
            //API manager 只暴露那些参数均为基本类型的方法
            private static bool CheckMethod(RestMethodInfo info)
            {
                foreach (RestParameterInfo pInfo in info.ParameterInfos)
                {
                    if (!pInfo.ParameterType.IsPrimitive &&
                        (pInfo.ParameterType != typeof(string)) &&
                        (pInfo.ParameterType != typeof(HttpPostedFile)))
                    {
                        throw new Exception("不支持的参数格式 资源名 " + info.ResourceName + " 参数名 " + pInfo.ParameterName);
                    }
                }

                return(true);
            }
Exemple #2
0
 /// <summary>
 /// 注册缓存信息
 /// </summary>
 /// <param name="info"></param>
 public void Register(RestMethodInfo info)
 {
     #region 生成缓存项
     if (RestCache.Enable)
     {
         CacheItem config = configuration.CacheItems.Find((item) => { return(info.ActionId == item.ActionId); });
         if (config != null)
         {
             info.ExpireSeconds = config.ExpireSeconds;
             info.IsCached      = config.IsCached;
         }
     }
     #endregion
 }
Exemple #3
0
 /// <summary>
 /// 注册缓存信息
 /// </summary>
 /// <param name="info"></param>
 public void Register(RestMethodInfo info)
 {
     #region 生成缓存项
     if (RestCache.Enable)
     {
         CacheItem config = configuration.CacheItems.Find((item) => { return info.ActionId == item.ActionId; });
         if (config != null)
         {
             info.ExpireSeconds = config.ExpireSeconds;
             info.IsCached = config.IsCached;
         }
     }
     #endregion
 }
Exemple #4
0
            //This method can only be called at the static constructor of APIMethodManager. CLR will keep the thread safety.
            internal static MethodInvoker CreateMethodInvoker(RestMethodInfo info, TypeBuilder tb)
            {
                if (!CheckMethod(info))
                {
                    return(null);
                }

                Type[]        argumentTypes = new Type[] { typeof(object), typeof(string[]) };
                MethodInvoker invoker       = new MethodInvoker();

                invoker.serverInstance = Activator.CreateInstance(info.ProxyMethodInfo.DeclaringType);
                invoker.info           = info;
                MethodBuilder mb = null;

                mb = tb.DefineMethod(info.MethodName, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, info.ReturnType, argumentTypes);
                ILGenerator il = mb.GetILGenerator();

                LocalBuilder[] locals    = new LocalBuilder[info.ParameterInfos.Count];
                Label[]        tryCatchs = new Label[info.ParameterInfos.Count];

                for (int i = 0; i < info.ParameterInfos.Count; i++)
                {
                    tryCatchs[i] = il.BeginExceptionBlock();
                    locals[i]    = il.DeclareLocal(info.ParameterInfos[i].ParameterType);
                    il.Emit(OpCodes.Ldarg_1);
                    LoadLiteral(il, i);
                    il.Emit(OpCodes.Ldelem_Ref);
                    object dValue = null;
                    if (!info.ParameterInfos[i].IsRequired)
                    {
                        dValue = info.ParameterInfos[i].DefaultValue;
                    }

                    if (info.ParameterInfos[i].ParameterType == typeof(string))
                    {
                        Label label = il.DefineLabel();
                        il.Emit(OpCodes.Dup);
                        il.Emit(OpCodes.Call, isNullOrEmpty);
                        il.Emit(OpCodes.Brfalse_S, label);
                        if (info.ParameterInfos[i].IsRequired)
                        {
                            il.Emit(OpCodes.Newobj, typeof(Exception).GetConstructor(new Type[] { }));
                            il.Emit(OpCodes.Throw);
                        }
                        else
                        {
                            il.Emit(OpCodes.Pop);
                            if (dValue == null)
                            {
                                il.Emit(OpCodes.Ldnull);
                            }
                            else
                            {
                                string v = dValue as string;
                                if (v == null)
                                {
                                    throw new Exception(string.Format("错误的默认值设定 method:{0} parameter:{1}", info.ResourceName, info.ParameterInfos[i].ParameterName));
                                }
                                il.Emit(OpCodes.Ldstr, v);
                            }
                        }
                        il.MarkLabel(label);
                    }
                    else if (info.ParameterInfos[i].ParameterType == typeof(int))
                    {
                        if (info.ParameterInfos[i].IsRequired)
                        {
                            il.Emit(OpCodes.Call, intParser);
                        }
                        else
                        {
                            int v = 0;
                            try
                            {
                                v = dValue == null ? 0 : Convert.ToInt32(dValue);
                            }
                            catch
                            {
                                throw new Exception(string.Format("错误的默认值设定 method:{0} parameter:{1}", info.ResourceName, info.ParameterInfos[i].ParameterName));
                            }
                            Label label1 = il.DefineLabel();
                            Label label2 = il.DefineLabel();
                            il.Emit(OpCodes.Dup);
                            il.Emit(OpCodes.Call, isNullOrEmpty);
                            il.Emit(OpCodes.Brtrue_S, label2);
                            il.Emit(OpCodes.Call, intParser);
                            il.Emit(OpCodes.Br_S, label1);
                            il.MarkLabel(label2);
                            il.Emit(OpCodes.Pop);
                            LoadLiteral(il, v);
                            il.MarkLabel(label1);
                        }
                    }
                    else if (info.ParameterInfos[i].ParameterType == typeof(long))
                    {
                        if (info.ParameterInfos[i].IsRequired)
                        {
                            il.Emit(OpCodes.Call, longParser);
                        }
                        else
                        {
                            long v = 0;
                            try
                            {
                                v = dValue == null ? 0 : Convert.ToInt64(dValue);
                            }
                            catch
                            {
                                throw new Exception(string.Format("错误的默认值设定 method:{0} parameter:{1}", info.ResourceName, info.ParameterInfos[i].ParameterName));
                            }

                            Label label1 = il.DefineLabel();
                            Label label2 = il.DefineLabel();
                            il.Emit(OpCodes.Dup);
                            il.Emit(OpCodes.Call, isNullOrEmpty);
                            il.Emit(OpCodes.Brtrue_S, label2);
                            il.Emit(OpCodes.Call, longParser);
                            il.Emit(OpCodes.Br_S, label1);
                            il.MarkLabel(label2);
                            il.Emit(OpCodes.Pop);
                            il.Emit(OpCodes.Ldc_I8, v);
                            il.MarkLabel(label1);
                        }
                    }
                    else if (info.ParameterInfos[i].ParameterType == typeof(double))
                    {
                        if (info.ParameterInfos[i].IsRequired)
                        {
                            il.Emit(OpCodes.Call, doubleParser);
                        }
                        else
                        {
                            double v = 0;
                            try
                            {
                                v = dValue == null ? 0 : Convert.ToDouble(dValue);
                            }
                            catch
                            {
                                throw new Exception(string.Format("错误的默认值设定 method:{0} parameter:{1}", info.ResourceName, info.ParameterInfos[i].ParameterName));
                            }

                            Label label1 = il.DefineLabel();
                            Label label2 = il.DefineLabel();
                            il.Emit(OpCodes.Dup);
                            il.Emit(OpCodes.Call, isNullOrEmpty);
                            il.Emit(OpCodes.Brtrue_S, label2);
                            il.Emit(OpCodes.Call, doubleParser);
                            il.Emit(OpCodes.Br_S, label1);
                            il.MarkLabel(label2);
                            il.Emit(OpCodes.Pop);
                            il.Emit(OpCodes.Ldc_R8, v);
                            il.MarkLabel(label1);
                        }
                    }
                    else if (info.ParameterInfos[i].ParameterType == typeof(bool))
                    {
                        if (info.ParameterInfos[i].IsRequired)
                        {
                            il.Emit(OpCodes.Call, boolParser);
                        }
                        else
                        {
                            bool v = false;
                            try
                            {
                                v = dValue == null ? false : Convert.ToBoolean(dValue);
                            }
                            catch
                            {
                                throw new Exception(string.Format("错误的默认值设定 method:{0} parameter:{1}", info.ResourceName, info.ParameterInfos[i].ParameterName));
                            }

                            Label label1 = il.DefineLabel();
                            Label label2 = il.DefineLabel();
                            il.Emit(OpCodes.Dup);
                            il.Emit(OpCodes.Call, isNullOrEmpty);
                            il.Emit(OpCodes.Brtrue_S, label2);
                            il.Emit(OpCodes.Call, boolParser);
                            il.Emit(OpCodes.Br_S, label1);
                            il.MarkLabel(label2);
                            il.Emit(OpCodes.Pop);
                            il.Emit(v ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
                            il.MarkLabel(label1);
                        }
                    }
                    else if (info.ParameterInfos[i].ParameterType == typeof(HttpPostedFile))
                    {
                        il.Emit(OpCodes.Call, getHttpContext);
                        il.Emit(OpCodes.Callvirt, getHttpRequest);
                        il.Emit(OpCodes.Callvirt, getRequestFiles);
                        il.Emit(OpCodes.Ldstr, info.ParameterInfos[i].ParameterName);
                        il.Emit(OpCodes.Callvirt, getFileItem);
                    }
                    else
                    {
                        throw new NotSupportedException("Donot support this parameters type in API, which is found in method : " + info.ResourceName);
                    }
                    il.Emit(OpCodes.Stloc, locals[i]);
                    il.Emit(OpCodes.Leave, tryCatchs[i]);
                    il.BeginCatchBlock(typeof(Exception));
                    il.Emit(OpCodes.Ldstr, info.ParameterInfos[i].ParameterName);
                    il.Emit(OpCodes.Ldarg_1);
                    LoadLiteral(il, i);
                    il.Emit(OpCodes.Ldelem_Ref);
                    il.Emit(OpCodes.Ldstr, info.ParameterInfos[i].ParameterType.FullName);
                    il.Emit(OpCodes.Newobj, typeof(RestParameterException).GetConstructor(new Type[] { typeof(string), typeof(string), typeof(string) }));
                    il.Emit(OpCodes.Throw);
                    il.EndExceptionBlock();
                }
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Castclass, info.ProxyMethodInfo.DeclaringType);
                for (int i = 0; i < locals.Length; i++)
                {
                    il.Emit(OpCodes.Ldloc, locals[i]);
                }
                il.Emit(OpCodes.Callvirt, info.ProxyMethodInfo);
                il.Emit(OpCodes.Ret);

                return(invoker);
            }
Exemple #5
0
            //API manager 只暴露那些参数均为基本类型的方法
            private static bool CheckMethod(RestMethodInfo info)
            {
                foreach (RestParameterInfo pInfo in info.ParameterInfos)
                {
                    if (!pInfo.ParameterType.IsPrimitive
                        && (pInfo.ParameterType != typeof(string))
                        && (pInfo.ParameterType != typeof(HttpPostedFile)))
                    {
                        throw new Exception("不支持的参数格式 资源名 " + info.ResourceName + " 参数名 " + pInfo.ParameterName);
                    }
                }

                return true;
            }
Exemple #6
0
            //This method can only be called at the static constructor of APIMethodManager. CLR will keep the thread safety.
            internal static MethodInvoker CreateMethodInvoker(RestMethodInfo info, TypeBuilder tb)
            {
                if (!CheckMethod(info)) return null;

                Type[] argumentTypes = new Type[] { typeof(object), typeof(string[]) };
                MethodInvoker invoker = new MethodInvoker();
                invoker.serverInstance = Activator.CreateInstance(info.ProxyMethodInfo.DeclaringType);
                invoker.info = info;
                MethodBuilder mb = null;
                mb = tb.DefineMethod(info.MethodName, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, info.ReturnType, argumentTypes);
                ILGenerator il = mb.GetILGenerator();
                LocalBuilder[] locals = new LocalBuilder[info.ParameterInfos.Count];
                Label[] tryCatchs = new Label[info.ParameterInfos.Count];

                for (int i = 0; i < info.ParameterInfos.Count; i++)
                {
                    tryCatchs[i] = il.BeginExceptionBlock();
                    locals[i] = il.DeclareLocal(info.ParameterInfos[i].ParameterType);
                    il.Emit(OpCodes.Ldarg_1);
                    LoadLiteral(il, i);
                    il.Emit(OpCodes.Ldelem_Ref);
                    object dValue = null;
                    if (!info.ParameterInfos[i].IsRequired)
                    {
                        dValue = info.ParameterInfos[i].DefaultValue;
                    }

                    if (info.ParameterInfos[i].ParameterType == typeof(string))
                    {
                        Label label = il.DefineLabel();
                        il.Emit(OpCodes.Dup);
                        il.Emit(OpCodes.Call, isNullOrEmpty);
                        il.Emit(OpCodes.Brfalse_S, label);
                        if (info.ParameterInfos[i].IsRequired)
                        {
                            il.Emit(OpCodes.Newobj, typeof(Exception).GetConstructor(new Type[] { }));
                            il.Emit(OpCodes.Throw);
                        }
                        else
                        {
                            il.Emit(OpCodes.Pop);
                            if (dValue == null)
                            {
                                il.Emit(OpCodes.Ldnull);
                            }
                            else
                            {
                                string v = dValue as string;
                                if (v == null) throw new Exception(string.Format("错误的默认值设定 method:{0} parameter:{1}", info.ResourceName, info.ParameterInfos[i].ParameterName));
                                il.Emit(OpCodes.Ldstr, v);
                            }
                        }
                        il.MarkLabel(label);
                    }
                    else if (info.ParameterInfos[i].ParameterType == typeof(int))
                    {
                        if (info.ParameterInfos[i].IsRequired)
                        {
                            il.Emit(OpCodes.Call, intParser);
                        }
                        else
                        {
                            int v = 0;
                            try
                            {
                                v = dValue == null ? 0 : Convert.ToInt32(dValue);
                            }
                            catch
                            {
                                throw new Exception(string.Format("错误的默认值设定 method:{0} parameter:{1}", info.ResourceName, info.ParameterInfos[i].ParameterName));
                            }
                            Label label1 = il.DefineLabel();
                            Label label2 = il.DefineLabel();
                            il.Emit(OpCodes.Dup);
                            il.Emit(OpCodes.Call, isNullOrEmpty);
                            il.Emit(OpCodes.Brtrue_S, label2);
                            il.Emit(OpCodes.Call, intParser);
                            il.Emit(OpCodes.Br_S, label1);
                            il.MarkLabel(label2);
                            il.Emit(OpCodes.Pop);
                            LoadLiteral(il, v);
                            il.MarkLabel(label1);
                        }
                    }
                    else if (info.ParameterInfos[i].ParameterType == typeof(long))
                    {
                        if (info.ParameterInfos[i].IsRequired)
                        {
                            il.Emit(OpCodes.Call, longParser);
                        }
                        else
                        {
                            long v = 0;
                            try
                            {
                                v = dValue == null ? 0 : Convert.ToInt64(dValue);
                            }
                            catch
                            {
                                throw new Exception(string.Format("错误的默认值设定 method:{0} parameter:{1}", info.ResourceName, info.ParameterInfos[i].ParameterName));
                            }

                            Label label1 = il.DefineLabel();
                            Label label2 = il.DefineLabel();
                            il.Emit(OpCodes.Dup);
                            il.Emit(OpCodes.Call, isNullOrEmpty);
                            il.Emit(OpCodes.Brtrue_S, label2);
                            il.Emit(OpCodes.Call, longParser);
                            il.Emit(OpCodes.Br_S, label1);
                            il.MarkLabel(label2);
                            il.Emit(OpCodes.Pop);
                            il.Emit(OpCodes.Ldc_I8, v);
                            il.MarkLabel(label1);
                        }

                    }
                    else if (info.ParameterInfos[i].ParameterType == typeof(double))
                    {
                        if (info.ParameterInfos[i].IsRequired)
                        {
                            il.Emit(OpCodes.Call, doubleParser);
                        }
                        else
                        {
                            double v = 0;
                            try
                            {
                                v = dValue == null ? 0 : Convert.ToDouble(dValue);
                            }
                            catch
                            {
                                throw new Exception(string.Format("错误的默认值设定 method:{0} parameter:{1}", info.ResourceName, info.ParameterInfos[i].ParameterName));
                            }

                            Label label1 = il.DefineLabel();
                            Label label2 = il.DefineLabel();
                            il.Emit(OpCodes.Dup);
                            il.Emit(OpCodes.Call, isNullOrEmpty);
                            il.Emit(OpCodes.Brtrue_S, label2);
                            il.Emit(OpCodes.Call, doubleParser);
                            il.Emit(OpCodes.Br_S, label1);
                            il.MarkLabel(label2);
                            il.Emit(OpCodes.Pop);
                            il.Emit(OpCodes.Ldc_R8, v);
                            il.MarkLabel(label1);
                        }
                    }
                    else if (info.ParameterInfos[i].ParameterType == typeof(bool))
                    {
                        if (info.ParameterInfos[i].IsRequired)
                        {
                            il.Emit(OpCodes.Call, boolParser);
                        }
                        else
                        {
                            bool v = false;
                            try
                            {
                                v = dValue == null ? false : Convert.ToBoolean(dValue);
                            }
                            catch
                            {
                                throw new Exception(string.Format("错误的默认值设定 method:{0} parameter:{1}", info.ResourceName, info.ParameterInfos[i].ParameterName));
                            }

                            Label label1 = il.DefineLabel();
                            Label label2 = il.DefineLabel();
                            il.Emit(OpCodes.Dup);
                            il.Emit(OpCodes.Call, isNullOrEmpty);
                            il.Emit(OpCodes.Brtrue_S, label2);
                            il.Emit(OpCodes.Call, boolParser);
                            il.Emit(OpCodes.Br_S, label1);
                            il.MarkLabel(label2);
                            il.Emit(OpCodes.Pop);
                            il.Emit(v ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
                            il.MarkLabel(label1);
                        }
                    }
                    else if (info.ParameterInfos[i].ParameterType == typeof(HttpPostedFile))
                    {
                        il.Emit(OpCodes.Call, getHttpContext);
                        il.Emit(OpCodes.Callvirt, getHttpRequest);
                        il.Emit(OpCodes.Callvirt, getRequestFiles);
                        il.Emit(OpCodes.Ldstr, info.ParameterInfos[i].ParameterName);
                        il.Emit(OpCodes.Callvirt, getFileItem);
                    }
                    else
                    {
                        throw new NotSupportedException("Donot support this parameters type in API, which is found in method : " + info.ResourceName);
                    }
                    il.Emit(OpCodes.Stloc, locals[i]);
                    il.Emit(OpCodes.Leave, tryCatchs[i]);
                    il.BeginCatchBlock(typeof(Exception));
                    il.Emit(OpCodes.Ldstr, info.ParameterInfos[i].ParameterName);
                    il.Emit(OpCodes.Ldarg_1);
                    LoadLiteral(il, i);
                    il.Emit(OpCodes.Ldelem_Ref);
                    il.Emit(OpCodes.Ldstr, info.ParameterInfos[i].ParameterType.FullName);
                    il.Emit(OpCodes.Newobj, typeof(RestParameterException).GetConstructor(new Type[] { typeof(string), typeof(string), typeof(string) }));
                    il.Emit(OpCodes.Throw);
                    il.EndExceptionBlock();
                }
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Castclass, info.ProxyMethodInfo.DeclaringType);
                for (int i = 0; i < locals.Length; i++)
                {
                    il.Emit(OpCodes.Ldloc, locals[i]);
                }
                il.Emit(OpCodes.Callvirt, info.ProxyMethodInfo);
                il.Emit(OpCodes.Ret);

                return invoker;
            }
Exemple #7
0
 /// <summary>
 /// 注册需进行Rest的类
 /// </summary>
 /// <param name="info"></param>
 internal void Register(RestMethodInfo info)
 {
     MethodInvoker invoker = MethodInvoker.CreateMethodInvoker(info, typeBuilder);
     if (invoker != null)
     {
         actionIdToMethod.Add(info.ActionId, invoker);
         nameActionMapping.Add(info.ActionName.ToLower(), info.ActionId);
     }
 }
Exemple #8
0
        /// <summary>
        /// 在rest管理中注册相应的rest类
        /// </summary>
        /// <param name="type">有Rest类属性标识的类</param>
        public virtual void Register(Type type)
        {
            RestAPIClassAttribute[] classAttributes = type.GetCustomAttributes(typeof(RestAPIClassAttribute), false) as RestAPIClassAttribute[];
            if (classAttributes.Length == 1)
            {
                DesignedErrorCodeAttribute[] cErrorCodeAttributes = type.GetCustomAttributes(typeof(DesignedErrorCodeAttribute), false) as DesignedErrorCodeAttribute[];

                foreach (MethodInfo info in type.GetMethods(BindingFlags.Public | BindingFlags.Instance))
                {
                    RestAPIMethodAttribute[] apiAttributes = info.GetCustomAttributes(typeof(RestAPIMethodAttribute), false) as RestAPIMethodAttribute[];
                    if (apiAttributes.Length == 1)
                    {
                        DesignedErrorCodeAttribute[] mErrorCodeAttributes = info.GetCustomAttributes(typeof(DesignedErrorCodeAttribute), false) as DesignedErrorCodeAttribute[];

                        if (!IsValidReturnType(info.ReturnType))
                        {
                            throw new Exception("不支持的返回值格式");
                        }
                        RestMethodInfo restInfo = new RestMethodInfo();
                        restInfo.ActionName      = classAttributes[0].ResourceName + "/" + apiAttributes[0].Name;
                        restInfo.ResourceName    = classAttributes[0].ResourceName + "/" + apiAttributes[0].Name + "(" + apiAttributes[0].ActionId + ")";
                        restInfo.ActionId        = apiAttributes[0].ActionId;
                        restInfo.Description     = apiAttributes[0].Title;
                        restInfo.MethodName      = classAttributes[0].ResourceName + "_" + apiAttributes[0].Name;;
                        restInfo.SecurityLevel   = apiAttributes[0].SecurityLevel;
                        restInfo.MaxLogLength    = apiAttributes[0].MaxLogLength;
                        restInfo.DefaultHandler  = apiAttributes[0].DefaultHandler;
                        restInfo.AccessPolicy    = apiAttributes[0].AccessPolicy;
                        restInfo.IpSecurityCheck = apiAttributes[0].IpSecurityCheck;
                        restInfo.EnableOsapCheck = apiAttributes[0].EnableOsapCheck;
                        restInfo.IsObsolete      = (info.GetCustomAttributes(typeof(ObsoleteAttribute), false) as ObsoleteAttribute[]).Length > 0;
                        restInfo.ProxyMethodInfo = info;
                        restInfo.ReturnType      = info.ReturnType;
                        restInfo.ParameterInfos  = new List <RestParameterInfo>();
                        #region 获取该接口可能抛出的异常的errorcode集合
                        List <int> errorCodes = new List <int>();
                        if (cErrorCodeAttributes.Length == 1 && cErrorCodeAttributes[0].Codes != null)
                        {
                            foreach (int i in cErrorCodeAttributes[0].Codes)
                            {
                                if (!errorCodes.Contains(i))
                                {
                                    errorCodes.Add(i);
                                }
                            }
                        }
                        if (mErrorCodeAttributes.Length == 1 && mErrorCodeAttributes[0].Codes != null)
                        {
                            foreach (int i in mErrorCodeAttributes[0].Codes)
                            {
                                if (!errorCodes.Contains(i))
                                {
                                    errorCodes.Add(i);
                                }
                            }
                        }
                        errorCodes.Sort();
                        restInfo.ErrorCodes = errorCodes.ToArray();
                        #endregion
                        manager.RestInfos.Add(restInfo.ActionId, restInfo);

                        object[] cacheAttributes             = info.GetCustomAttributes(false);
                        CachedMethodAttribute cacheAttribute = null;

                        foreach (object o in cacheAttributes)
                        {
                            cacheAttribute = o as CachedMethodAttribute;

                            if (cacheAttribute != null)
                            {
                                break;
                            }
                        }



                        if (RestCache.Enable && cacheAttribute != null)
                        {
                            restInfo.IsCached        = cacheAttribute.IsCached;
                            restInfo.ExpireSeconds   = cacheAttribute.DefaultExpireSeconds;
                            restInfo.CacheMethodAttr = cacheAttribute;
                        }
                        else
                        {
                            restInfo.IsCached = false;
                        }

                        foreach (ParameterInfo parameter in info.GetParameters())
                        {
                            RestParameterInfo pInfo = new RestParameterInfo();
                            pInfo.ParameterName = parameter.Name;
                            pInfo.ParameterType = parameter.ParameterType;

                            CachedKeyAttribute[]     keyAttributes      = parameter.GetCustomAttributes(typeof(CachedKeyAttribute), false) as CachedKeyAttribute[];
                            RestParameterAttribute[] requiredAttributes = parameter.GetCustomAttributes(typeof(RestParameterAttribute), false) as RestParameterAttribute[];
                            if (keyAttributes.Length > 0)
                            {
                                CacheKey key = new CacheKey();
                            }
                            if (requiredAttributes.Length > 0)
                            {
                                if (requiredAttributes[0].DefaultValue != null)
                                {
                                    if (!pInfo.ParameterType.Equals(requiredAttributes[0].DefaultValue.GetType()))
                                    {
                                        throw new Exception(string.Format("错误的默认值设定 resouce:{0} parameter:{1}", restInfo.ResourceName, parameter.Name));
                                    }
                                    //设置默认的值
                                    pInfo.DefaultValue = requiredAttributes[0].DefaultValue;
                                }
                                pInfo.Description = requiredAttributes[0].Description;
                                pInfo.IsRequired  = requiredAttributes[0].IsRequired;
                            }
                            restInfo.ParameterInfos.Add(pInfo);
                        }
                        manager.Register(restInfo);
                        if (RestCache.Enable)
                        {
                            cache.Register(restInfo);
                        }
                    }
                }
            }
        }
Exemple #9
0
        /// <summary>
        /// 在rest管理中注册相应的rest类
        /// </summary>
        /// <param name="type">有Rest类属性标识的类</param>
        public virtual void Register(Type type)
        {
            RestAPIClassAttribute[] classAttributes = type.GetCustomAttributes(typeof(RestAPIClassAttribute), false) as RestAPIClassAttribute[];
            if (classAttributes.Length == 1)
            {
                DesignedErrorCodeAttribute[] cErrorCodeAttributes = type.GetCustomAttributes(typeof(DesignedErrorCodeAttribute), false) as DesignedErrorCodeAttribute[];

                foreach (MethodInfo info in type.GetMethods(BindingFlags.Public | BindingFlags.Instance))
                {
                    RestAPIMethodAttribute[] apiAttributes = info.GetCustomAttributes(typeof(RestAPIMethodAttribute), false) as RestAPIMethodAttribute[];
                    if (apiAttributes.Length == 1)
                    {
                        DesignedErrorCodeAttribute[] mErrorCodeAttributes = info.GetCustomAttributes(typeof(DesignedErrorCodeAttribute), false) as DesignedErrorCodeAttribute[];

                        if (!IsValidReturnType(info.ReturnType))
                            throw new Exception("不支持的返回值格式");
                        RestMethodInfo restInfo = new RestMethodInfo();
                        restInfo.ActionName = classAttributes[0].ResourceName + "/" + apiAttributes[0].Name;
                        restInfo.ResourceName = classAttributes[0].ResourceName + "/" + apiAttributes[0].Name + "(" + apiAttributes[0].ActionId + ")";
                        restInfo.ActionId = apiAttributes[0].ActionId;
                        restInfo.Description = apiAttributes[0].Title;
                        restInfo.MethodName = classAttributes[0].ResourceName + "_" + apiAttributes[0].Name; ;
                        restInfo.SecurityLevel = apiAttributes[0].SecurityLevel;
                        restInfo.MaxLogLength = apiAttributes[0].MaxLogLength;
                        restInfo.DefaultHandler = apiAttributes[0].DefaultHandler;
                        restInfo.AccessPolicy = apiAttributes[0].AccessPolicy;
                        restInfo.IpSecurityCheck = apiAttributes[0].IpSecurityCheck;
                        restInfo.EnableOsapCheck = apiAttributes[0].EnableOsapCheck;
                        restInfo.IsObsolete = (info.GetCustomAttributes(typeof(ObsoleteAttribute), false) as ObsoleteAttribute[]).Length > 0;
                        restInfo.ProxyMethodInfo = info;
                        restInfo.ReturnType = info.ReturnType;
                        restInfo.ParameterInfos = new List<RestParameterInfo>();
                        #region 获取该接口可能抛出的异常的errorcode集合
                        List<int> errorCodes = new List<int>();
                        if (cErrorCodeAttributes.Length == 1 && cErrorCodeAttributes[0].Codes != null)
                        {
                            foreach (int i in cErrorCodeAttributes[0].Codes)
                            {
                                if (!errorCodes.Contains(i))
                                {
                                    errorCodes.Add(i);
                                }
                            }
                        }
                        if (mErrorCodeAttributes.Length == 1 && mErrorCodeAttributes[0].Codes != null)
                        {
                            foreach (int i in mErrorCodeAttributes[0].Codes)
                            {
                                if (!errorCodes.Contains(i))
                                {
                                    errorCodes.Add(i);
                                }
                            }
                        }
                        errorCodes.Sort();
                        restInfo.ErrorCodes = errorCodes.ToArray();
                        #endregion
                        manager.RestInfos.Add(restInfo.ActionId, restInfo);

                        object[] cacheAttributes = info.GetCustomAttributes(false);
                        CachedMethodAttribute cacheAttribute = null;

                        foreach (object o in cacheAttributes)
                        {
                            cacheAttribute = o as CachedMethodAttribute;

                            if (cacheAttribute != null)
                                break;
                        }

                        if (RestCache.Enable && cacheAttribute != null)
                        {
                            restInfo.IsCached = cacheAttribute.IsCached;
                            restInfo.ExpireSeconds = cacheAttribute.DefaultExpireSeconds;
                            restInfo.CacheMethodAttr = cacheAttribute;
                        }
                        else
                        {
                            restInfo.IsCached = false;
                        }

                        foreach (ParameterInfo parameter in info.GetParameters())
                        {
                            RestParameterInfo pInfo = new RestParameterInfo();
                            pInfo.ParameterName = parameter.Name;
                            pInfo.ParameterType = parameter.ParameterType;

                            CachedKeyAttribute[] keyAttributes = parameter.GetCustomAttributes(typeof(CachedKeyAttribute), false) as CachedKeyAttribute[];
                            RestParameterAttribute[] requiredAttributes = parameter.GetCustomAttributes(typeof(RestParameterAttribute), false) as RestParameterAttribute[];
                            if (keyAttributes.Length > 0)
                            {
                                CacheKey key = new CacheKey();
                            }
                            if (requiredAttributes.Length > 0)
                            {
                                if (requiredAttributes[0].DefaultValue != null)
                                {
                                    if (!pInfo.ParameterType.Equals(requiredAttributes[0].DefaultValue.GetType()))
                                    {
                                        throw new Exception(string.Format("错误的默认值设定 resouce:{0} parameter:{1}", restInfo.ResourceName, parameter.Name));
                                    }
                                    //设置默认的值
                                    pInfo.DefaultValue = requiredAttributes[0].DefaultValue;
                                }
                                pInfo.Description = requiredAttributes[0].Description;
                                pInfo.IsRequired = requiredAttributes[0].IsRequired;
                            }
                            restInfo.ParameterInfos.Add(pInfo);
                        }
                        manager.Register(restInfo);
                        if (RestCache.Enable)
                            cache.Register(restInfo);
                    }
                }
            }
        }