Esempio n. 1
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);
                        }
                    }
                }
            }
        }
Esempio n. 2
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);
                    }
                }
            }
        }