Esempio n. 1
0
        private void ProcessHander(HttpContext context, IWebHandlerInfo webHandlerInfo)
        {
            using (var codeWatch = new CodeWatch("IJsonHandler ProcessRequest", 2000,
                                                 new Action <string, LoggerStrategyBase, int?, long>(
                                                     (tag, currentLog, wcount, execms) =>
                                                     currentLog.LogFormat(LoggerLevels.Warn, "\t{0}:JSONHandler({3})执行时间为({1})ms.已超过阀值({2})ms.",
                                                                          tag,
                                                                          execms,
                                                                          wcount,
                                                                          context.Request.Url.AbsolutePath))))
            {
                if (WebContext == null)
                {
                    WebContext = new WebContext();
                }
                switch (webHandlerInfo.WebHandlerType)
                {
                case WebHandlerTypes.JSONHandler:
                {
                    var handler    = (IJSONHandler)Activator.CreateInstance(webHandlerInfo.HandlerInstance);
                    var jsonReturn = handler.ProcessJSONRequest(WebContext);
                    if (jsonReturn != null && jsonReturn.IsResponseJsonObject)
                    {
                        context.Response.ContentType = "application/json";
                        //输出JSON对象
                        context.Response.Write(jsonReturn.HasSetJsonObject
                                    ? jsonReturn.JsonObject.ToJson(jsonReturn.JSONCommonFormatHanding)
                                    : new { IsSuccess = true }.ToJson(jsonReturn.JSONCommonFormatHanding));
                    }
                }
                break;

                case WebHandlerTypes.ResourceHandler:
                    break;

                case WebHandlerTypes.WebAPIHandler:
                {
                    context.Response.ContentType = "application/json";
                    var result = new OperationResult()
                    {
                        ResultType = OperationResultType.Success,
                        Message    = "操作成功!"
                    };
                    var handler = (IWebAPI)Activator.CreateInstance(webHandlerInfo.HandlerInstance);
                    handler.OperationResult = result;
                    try
                    {
                        switch (WebContext.HttpContext.Request.HttpMethod)
                        {
                        case "GET":
                        {
                            var paramStrs = WebAPIHelper.GetUrlParams(webHandlerInfo.WebAPIName);
                            if (paramStrs != null && paramStrs.Length > 0)
                            {
                                if (paramStrs.Length > 1)
                                {
                                    result = handler.Get(paramStrs);
                                }
                                else
                                {
                                    result = handler.Get(paramStrs[0]);
                                }
                            }
                            else
                            {
                                result = handler.Get(paramStrs);
                            }
                            //输出JSON对象
                            context.Response.Write(result.Data.ToJsonWithDateFormatyyyyMMddHHmmss());
                            return;
                        }

                        case "POST":
                        {
                            result = handler.Post(WebAPIHelper.GetPostJSON());
                            break;
                        }

                        case "PUT":
                        {
                            var id = WebAPIHelper.GetUrlId(webHandlerInfo.WebAPIName);
                            if (id.IsEmpty())
                            {
                                result.ResultType = OperationResultType.ParamError;
                                result.Message    = "参数错误!";
                                result.LogMessage = "Id不能为空!";
                            }
                            result = handler.Put(id, WebAPIHelper.GetPostJSON());
                            break;
                        }

                        case "DELETE":
                        {
                            var id = WebAPIHelper.GetUrlId(webHandlerInfo.WebAPIName);
                            if (id.IsEmpty())
                            {
                                result.ResultType = OperationResultType.ParamError;
                                result.Message    = "参数错误!";
                                result.LogMessage = "Id不能为空!";
                            }
                            result = handler.Delete(id);
                            break;
                        }

                        default:
                            break;
                        }
                    }
                    catch (NotImplementedException ex)
                    {
                        result.ResultType = OperationResultType.NoImplemented;
                        result.Message    = "该接口尚未实现!";
                        result.LogMessage = "该接口尚未实现!";
                    }
                    catch (Exception ex)
                    {
                        result.ResultType = OperationResultType.Error;
                        result.Message    = "意外错误,请联系管理员!";
                    }

                    //输出JSON对象
                    context.Response.Write(result.ToJsonWithDateFormatyyyyMMddHHmmss());
                }
                break;

                default:
                    break;
                }
            }
        }