예제 #1
0
        public StringBuilder GetSvr()
        {
            Type type = this.GetType();

            System.Uri url = HttpContext.Current.Request.Url;

            string script = GetScriptTemplete();

            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.Desc);
                scriptBuilder.AppendLine("附加说明:缓存时间 " + resAnn.CacheDuration.ToString() + " 秒");
                scriptBuilder.AppendLine("         输出类型 " + resAnn.ResponseFormat.ToString());
                scriptBuilder.AppendLine("----------------------------------------------------------------------------*/");

                string func = GetFunctionTemplete(m, resAnn.ResponseFormat);
                scriptBuilder.AppendLine(func);
            }

            return(scriptBuilder);
        }
예제 #2
0
        /// <summary>
        /// 设置缓存策略
        /// </summary>
        /// <param name="context"></param>
        /// <param name="resAnn"></param>
        private static void InitializeCachePolicy(HttpContext context, ResponseAnnotationAttribute resAnn)
        {
            int cacheDuration = resAnn.CacheDuration;

            if (cacheDuration > 0)
            {
                context.Response.Cache.SetCacheability(HttpCacheability.Server);
                context.Response.Cache.SetExpires(DateTime.Now.AddSeconds(
                                                      (double)cacheDuration));
                context.Response.Cache.SetSlidingExpiration(false);
                context.Response.Cache.SetValidUntilExpires(true);

                if (resAnn.Params > 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
        /// <summary>
        /// 取得方法的返回值标注属性
        /// </summary>
        /// <param name="methodName"></param>
        /// <returns></returns>
        private ResponseAnnotationAttribute GetAnnation(string methodName)
        {
            MethodInfo methInfo = this.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.Public);

            ResponseAnnotationAttribute[] resAnns = (ResponseAnnotationAttribute[])methInfo.GetCustomAttributes(typeof(ResponseAnnotationAttribute), false);

            ResponseAnnotationAttribute ann = null;

            if (resAnns.Length == 0)
            {
                ann = ResponseAnnotationAttribute.Default;
            }
            else
            {
                ann = resAnns[0];
            }

            ann = (ann as ICloneable).Clone() as ResponseAnnotationAttribute;

            ParameterInfo[] ps = methInfo.GetParameters();

            if (ps != null)
            {
                ann.Params = ps.Length;
            }


            return(ann);
        }
        public object Clone()
        {
            ResponseAnnotationAttribute obj = new ResponseAnnotationAttribute();

            obj.ResponseFormat = this.ResponseFormat;
            obj.Params         = this.Params;
            obj.CacheDuration  = this._caccheDuration;
            obj.Desc           = this._desc;
            return(obj);
        }
예제 #5
0
        /// <summary>
        /// 处理客户端请求
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            string contextMethod = "";

            #region 解码阶段
            WebRequestDecoder decoder = null;

            try
            {
                decoder = WebRequestDecoder.CreateInstance(context);
            }
            catch (NotImplementedException)
            {
                decoder = this.OnDecodeResolve(context);

                if (decoder == null)
                {
                    throw new NotSupportedException("无法为当前的请求提供适当的解码器");
                }
            }

            contextMethod = decoder.LogicalMethod;
            #endregion

            #region 调用阶段
            object result = this.Invoke(contextMethod, decoder.Deserialize());

            ResponseAnnotationAttribute resAnn = this.GetAnnation(contextMethod);

            #endregion

            #region 编码阶段
            WebResponseEncoder encoder = null;
            try
            {
                encoder = WebResponseEncoder.CreateInstance(context, resAnn.ResponseFormat);
            }
            catch (NotImplementedException)
            {
                encoder = this.OnEncoderResolve(context, contextMethod, resAnn);

                if (encoder == null)
                {
                    throw new NotSupportedException("无法为当前的请求提供适当的编码器");
                }
            }

            #region 处理化自定义的序列化机制
            OnSerializeHandler handler = this.OnGetCustomerSerializer(contextMethod);

            if (handler != null)
            {
                encoder.OnSerialize += handler;
            }
            #endregion
            #endregion

            #region 回复阶段
            encoder.Write(result);

            InitializeCachePolicy(context, resAnn);

            #endregion
        }
예제 #6
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);
 }