/// <summary>
        /// 实现Get方式Jsonp响应
        /// </summary>
        /// <param name="kind"></param>
        /// <param name="method"></param>
        /// <returns></returns>
        public Stream GetEntryCallBack(string kind, string method)
        {
            var request = WebOperationContext.Current.IncomingRequest;
            var response = WebOperationContext.Current.OutgoingResponse;

            var query = request.UriTemplateMatch.QueryParameters;
            var callback = query["callback"];
            string result = string.Empty;

            if (string.IsNullOrEmpty(callback))
            {
                var ret = new RESTfulResult { Code = (int)HttpStatusCode.OK, Message = "Not found [callback] parameter!" };
                //throw new WebFaultException<RESTfulResult>(ret, HttpStatusCode.Forbidden);
                response.StatusCode = HttpStatusCode.BadRequest;
                response.ContentType = "application/json;charset=utf-8";
                result = SerializationManager.SerializeJson(ret);
            }
            else
            {
                result = GetResponseString(ParameterFormat.Jsonp, kind, method, query);
                response.ContentType = "application/javascript;charset=utf-8";
                result = string.Format("{0}({1});", callback, result ?? "{}");
            }

            return new MemoryStream(Encoding.UTF8.GetBytes(result));
        }
        /// <summary>
        /// 进行认证
        /// </summary>
        /// <returns></returns>
        private RESTfulResult AuthorizeRequest()
        {
            var response = WebOperationContext.Current.OutgoingResponse;
            response.StatusCode = HttpStatusCode.Unauthorized;

            //认证成功,设置上下文
            AuthorizeContext.Current = new AuthorizeContext
            {
                OperationContext = WebOperationContext.Current,
                HttpContext = HttpContext.Current
            };

            //实例化一个结果
            var restResult = new RESTfulResult { Code = (int)response.StatusCode };

            try
            {
                var token = Authorize();
                if (token.Succeed)
                {
                    response.StatusCode = HttpStatusCode.OK;

                    //认证成功
                    restResult.Code = (int)response.StatusCode;
                    restResult.Message = "Authentication request success.";

                    //认证信息
                    AuthorizeContext.Current.Token = token;
                }
                else
                {
                    restResult.Message = "Authentication request fail.";
                }
            }
            catch (AuthorizeException ex)
            {
                restResult.Code = ex.Code;
                restResult.Message = ex.Message;
            }
            catch (Exception ex)
            {
                restResult.Message = ex.Message;
            }

            return restResult;
        }
        /// <summary>
        /// 获取错误消息
        /// </summary>
        /// <param name="exception"></param>
        /// <param name="kind"></param>
        /// <param name="method"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        private string GetErrorMessage(Exception exception, string kind, string method, NameValueCollection nvs, out RESTfulResult ret)
        {
            var response = WebOperationContext.Current.OutgoingResponse;
            var request = WebOperationContext.Current.IncomingRequest;

            int code = (int)HttpStatusCode.BadRequest;
            if (exception is RESTfulException)
            {
                code = (exception as RESTfulException).Code;
            }

            //转换状态码
            response.StatusCode = (HttpStatusCode)code;

            //设置返回值
            ret = new RESTfulResult { Code = code, Message = ErrorHelper.GetInnerException(exception).Message };

            var errorMessage = string.Format("\r\n\tCode:[{0}]\r\n\tError:[{1}]\r\n\tMethod:[{2}.{3}]", code,
                    ErrorHelper.GetInnerException(exception).Message, kind, method);

            //如果参数大于0
            var coll = request.UriTemplateMatch.QueryParameters;

            //请求地址
            errorMessage = string.Format("{0}\r\n\tRequest Uri:{1}", errorMessage, GetRequestUri());

            if (request.Method.ToUpper() == "POST")
            {
                errorMessage = string.Format("{0}\r\n\tGET Parameters:{1}\r\n\tPOST Parameters:{2}",
                                                errorMessage, GetParameters(coll), GetParameters(nvs));
            }
            else
            {
                errorMessage = string.Format("{0}\r\n\tGET Parameters:{1}", errorMessage, GetParameters(coll));
            }

            //加上认证的用户名
            if (AuthorizeContext.Current != null && AuthorizeContext.Current.Token.Succeed)
            {
                errorMessage = string.Format("{0}\r\n\tUser:[{1}]", errorMessage, AuthorizeContext.Current.Token.Name);
            }

            //返回结果
            return errorMessage;
        }
        private string GetResponseString(ParameterFormat format, string kind, string method, NameValueCollection parameters)
        {
            var request = WebOperationContext.Current.IncomingRequest;
            var response = WebOperationContext.Current.OutgoingResponse;

            if (format == ParameterFormat.Json)
                response.ContentType = "application/json;charset=utf-8";
            else if (format == ParameterFormat.Xml)
                response.ContentType = "text/xml;charset=utf-8";
            else if (format == ParameterFormat.Text)
                response.ContentType = "text/plain;charset=utf-8";
            else if (format == ParameterFormat.Html)
                response.ContentType = "text/html;charset=utf-8";

            //从缓存读取
            object result = null;
            if (Context != null && !Context.Contains(kind, method))
            {
                response.StatusCode = HttpStatusCode.NotFound;
                result = new RESTfulResult { Code = (int)response.StatusCode, Message = "service [" + kind + "." + method + "] not found." };
            }
            else
            {

                //进行认证处理
                RESTfulResult authResult = new RESTfulResult { Code = (int)HttpStatusCode.OK };

                //进行认证处理
                if (Context != null && Context.IsAuthorized(kind, method))
                {
                    authResult = AuthorizeRequest();
                }

                //认证成功
                if (authResult.Code == (int)HttpStatusCode.OK)
                {
                    try
                    {
                        Type retType;
                        result = Context.Invoke(kind, method, parameters, out retType);

                        //设置返回成功
                        response.StatusCode = HttpStatusCode.OK;

                        //xml方式需要进行数据包装
                        if (format == ParameterFormat.Xml)
                        {
                            //如果是值类型,则以对象方式返回
                            if (retType.IsValueType || retType == typeof(string))
                            {
                                result = new RESTfulResponse { Value = result };
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        RESTfulResult ret;
                        var errorMessage = GetErrorMessage(ex, kind, method, parameters, out ret);

                        result = ret;

                        //重新定义一个异常
                        var error = new Exception(errorMessage, ex);

                        //记录错误日志
                        SimpleLog.Instance.WriteLogForDir("RESTful\\" + kind, error);
                    }
                    finally
                    {
                        //使用完后清理上下文
                        AuthorizeContext.Current = null;
                    }
                }
                else
                {
                    result = authResult;
                }
            }

            ISerializer serializer = SerializerFactory.Create(format);
            return serializer.Serialize(result);
        }