예제 #1
0
        /// <summary>
        /// 取得方法的返回值标注属性
        /// </summary>
        /// <param name="methodName"></param>
        /// <returns></returns>
        private ResponseAnnotationAttribute GetAnnation(string methodName)
        {
            MethodInfo methInfo;

            if (ReflectionCache.GetInstance().Get(methodName) == null)
            {
                methInfo = this.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public);
                ReflectionCache.GetInstance().Add(methodName, methInfo);
            }
            else
            {
                methInfo = ReflectionCache.GetInstance().Get(methodName);
            }
            if (methInfo == null)
            {
                throw new Exception("指定的逻辑方法不存在");
            }
            ResponseAnnotationAttribute[] resAnns = (ResponseAnnotationAttribute[])methInfo.GetCustomAttributes(typeof(ResponseAnnotationAttribute), false);
            ResponseAnnotationAttribute   ann     = null;

            ann = resAnns.Length == 0 ? ResponseAnnotationAttribute.Default : resAnns[0];
            ann = (ann as ICloneable).Clone() as ResponseAnnotationAttribute;
            ParameterInfo[] ps = methInfo.GetParameters();
            if (ps != null)
            {
                ann.Parameter = ps.Length;
            }
            return(ann);
        }
예제 #2
0
        /// <summary>
        /// 设置缓存策略
        /// </summary>
        /// <param name="context"></param>
        /// <param name="resAnn"></param>
        private static void InitializeCachePolicy(HttpContext context, ResponseAnnotationAttribute resAnn)
        {
            int cacheDuration = resAnn.CacheDurtion;

            if (cacheDuration > 0)
            {
                context.Response.Cache.SetCacheability(HttpCacheability.Server);
                context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(cacheDuration));
                context.Response.Cache.SetSlidingExpiration(false);
                context.Response.Cache.SetValidUntilExpires(true);
                if (resAnn.Parameter > 0)
                {
                    context.Response.Cache.VaryByParams["*"] = true;
                }
                else
                {
                    context.Response.Cache.VaryByParams.IgnoreParams = true;
                }
            }
            else
            {
                context.Response.Cache.SetNoServerCaching();
                context.Response.Cache.SetMaxAge(TimeSpan.Zero);
            }
        }
예제 #3
0
        public object Clone()
        {
            ResponseAnnotationAttribute responseAnnotationAttribute = new ResponseAnnotationAttribute();

            responseAnnotationAttribute.ResponseFormat    = ResponseFormat;
            responseAnnotationAttribute.HttpMethod        = HttpMethod;
            responseAnnotationAttribute.CacheDurtion      = CacheDurtion;
            responseAnnotationAttribute.Parameter         = Parameter;
            responseAnnotationAttribute.MethodDescription = MethodDescription;
            return(responseAnnotationAttribute);
        }
예제 #4
0
        public StringBuilder GetSvr()
        {
            Type   type   = this.GetType();
            Uri    url    = HttpContext.Current.Request.Url;
            string script = string.Empty;

            if (!string.IsNullOrEmpty(ReflectionCache.GetInstance().JsFile))
            {
                script = ReflectionCache.GetInstance().JsFile;
            }
            else
            {
                ReflectionCache.GetInstance().JsFile = GetScriptTemplete();
                script = ReflectionCache.GetInstance().JsFile;
            }
            script = script.Replace("%H_DESC%", "通过jQuery.ajax完成服务端函数调用");
            script = script.Replace("%H_DATE%", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            script = script.Replace("%URL%", url.Query.Length > 0 ? url.ToString().Replace(url.Query, "") : url.ToString());
            script = script.Replace("%CLS%", type.Name);

            StringBuilder scriptBuilder = new StringBuilder(script);

            MethodInfo[] methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            foreach (MethodInfo m in methods)
            {
                ResponseAnnotationAttribute resAnn = this.GetAnnation(m.Name);

                scriptBuilder.AppendLine("/*");
                scriptBuilder.AppendLine("功能说明:" + resAnn.MethodDescription);
                scriptBuilder.AppendLine("附加说明:缓存时间 " + resAnn.CacheDurtion.ToString() + " 秒");
                scriptBuilder.AppendLine("输出类型 " + resAnn.ResponseFormat.ToString());
                scriptBuilder.AppendLine("*/");
                if (string.IsNullOrEmpty(resAnn.HttpMethod))
                {
                    resAnn.HttpMethod = "POST";
                }
                string func = GetFunctionTemplete(m, resAnn.ResponseFormat, resAnn.HttpMethod);
                scriptBuilder.AppendLine(func);
            }
            return(scriptBuilder);
        }
 public object Clone()
 {
     ResponseAnnotationAttribute responseAnnotationAttribute = new ResponseAnnotationAttribute();
     responseAnnotationAttribute.ResponseFormat = ResponseFormat;
     responseAnnotationAttribute.HttpMethod = HttpMethod;
     responseAnnotationAttribute.CacheDurtion = CacheDurtion;
     responseAnnotationAttribute.Parameter = Parameter;
     responseAnnotationAttribute.MethodDescription = MethodDescription;
     return responseAnnotationAttribute;
 }
        /// <summary>
        /// 设置缓存策略
        /// </summary>
        /// <param name="context"></param>
        /// <param name="resAnn"></param>
        private static void InitializeCachePolicy(HttpContext context, ResponseAnnotationAttribute resAnn)
        {
            int cacheDuration = resAnn.CacheDurtion;

            if (cacheDuration > 0)
            {
                context.Response.Cache.SetCacheability(HttpCacheability.Server);
                context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(cacheDuration));
                context.Response.Cache.SetSlidingExpiration(false);
                context.Response.Cache.SetValidUntilExpires(true);
                if (resAnn.Parameter > 0)
                {
                    context.Response.Cache.VaryByParams["*"] = true;
                }
                else
                {
                    context.Response.Cache.VaryByParams.IgnoreParams = true;
                }
            }
            else
            {
                context.Response.Cache.SetNoServerCaching();
                context.Response.Cache.SetMaxAge(TimeSpan.Zero);
            }
        }
 /// <summary>
 /// 当默认的编码器配置无法提供的时候,根据上下文解析新的编码器
 /// </summary>
 /// <param name="context"></param>
 /// <param name="contextMethod"></param>
 /// <param name="resAnn"></param>
 /// <returns></returns>
 protected virtual WebResponseEncoder OnEncoderResolve(HttpContext context, string contextMethod, ResponseAnnotationAttribute resAnn)
 {
     if (contextMethod == "GetSvr")
     {
         return new JQueryScriptEncoder(context);
     }
     return null;
 }
예제 #8
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            #region 获取所有的方法
            //IPermissionDataAccess permissionDataAccess = new PermissionDataAccess();
            //var methods = this.GetType().GetMethods();
            //foreach (MethodInfo method in methods)
            //{
            //    ResponseAnnotationAttribute[] resAnns = (ResponseAnnotationAttribute[])method.GetCustomAttributes(typeof(ResponseAnnotationAttribute), false);
            //    ResponseAnnotationAttribute ann = null;
            //    ann = resAnns.Length == 0 ? ResponseAnnotationAttribute.Default : resAnns[0];
            //    ann = (ann as ICloneable).Clone() as ResponseAnnotationAttribute;
            //    Permission a = new Permission();
            //    a.PermissionId = KeyGenerator.Instance.GetNextValue("Permission");
            //    a.Code = method.Name;
            //    a.Fullname = ann.MethodDescription;
            //    permissionDataAccess.Insert(a);
            //}
            #endregion

            string contextMethod = string.Empty;
            //解码:get或者post的参数解析
            WebRequestDecoder decoder;
            try
            {
                decoder = WebRequestDecoder.CreateInstance(context);
            }
            catch (NotImplementedException)
            {
                decoder = OnDecodeResolve(context);
                if (decoder == null)
                {
                    throw new NotSupportedException("无法为当前的请求提供适当的解码器");
                }
            }
            contextMethod = decoder.LogicalMethodName;
            //登录操作超时判断
            if (contextMethod != "Login" && contextMethod != "GetSvr" && !IsLogined)
            {
                context.Response.Write("-100");
                return;
            }
            //权限判断
            //if (!PermissionSingleton.Instance.CheckPermission(User.RoleId, contextMethod))
            if (false)
            {
                context.Response.Write("-101");
                return;
            }

            //反射调用,返回结果
            object result = Invoke(contextMethod, decoder.Deserialize());
            ResponseAnnotationAttribute resAnn = GetAnnation(contextMethod);
            //编码阶段
            WebResponseEncoder encoder;
            try
            {
                encoder = WebResponseEncoder.CreateInstance(context, resAnn.ResponseFormat);
            }
            catch (NotImplementedException)
            {
                encoder = OnEncoderResolve(context, contextMethod, resAnn);
                if (encoder == null)
                {
                    throw new NotSupportedException("无法为当前的请求提供适当的编码器");
                }
            }
            OnSerializeHandler handler = OnGetCustomerSerializer(contextMethod);
            if (handler != null)
            {
                encoder.OnSerialize += handler;
            }
            //回复
            encoder.Write(result);
            InitializeCachePolicy(context, resAnn);
        }
예제 #9
0
 /// <summary>
 /// 当默认的编码器配置无法提供的时候,根据上下文解析新的编码器
 /// </summary>
 /// <param name="context"></param>
 /// <param name="contextMethod"></param>
 /// <param name="resAnn"></param>
 /// <returns></returns>
 protected virtual WebResponseEncoder OnEncoderResolve(HttpContext context, string contextMethod, ResponseAnnotationAttribute resAnn)
 {
     if (contextMethod == "GetSvr")
     {
         return(new JQueryScriptEncoder(context));
     }
     return(null);
 }