WebApi系列~基於單請求封裝多請求的設計

回到目錄html

怎麼說,單請求封裝多請求,這句話確實有點繞了,但仍是要看清楚,想明白這究竟是怎麼一回事,單請求即一次請求(get,post,put,delete),封閉多請求,即在客戶端發送的一個請求中可能包含多個子請求(真實的請求,接口),這種設計確實看着很靈活,客戶端能夠根據本身的須要去拿服務器的數據,確實不錯!web

首先咱們要定義一套本身的請求和響應對象json

#region 請求對象
    /// <summary>
    /// 參數對象
    /// </summary>
    [DataContractAttribute]
    public class RequestParam
    {
        public RequestParam(string name, string value)
        {
            this.ParamName = name;
            this.ParamValue = value;
        }
        [DataMemberAttribute]
        public string ParamName { get; private set; }
        [DataMemberAttribute]
        public string ParamValue { get; private set; }
    }
    /// <summary>
    /// 數據包中的實體
    /// </summary>
    [DataContractAttribute]
    public class RequestData
    {
        public RequestData()
        {
            this.HttpMethod = 0;
            this.RequestParam = new Dictionary<string, string>();
        }
        /// <summary>
        /// 本次通信惟一標示
        /// </summary>
        [DataMemberAttribute]
        public string GuidKey { get; set; }
        /// <summary>
        /// 請求方式0:get,1:Post
        /// </summary>
        public int HttpMethod { get; set; }
        /// <summary>
        /// 要調用的方法
        /// </summary>
        [DataMemberAttribute]
        public string Url { get; set; }
        /// <summary>
        /// 方法的參數列表
        /// </summary>
        [DataMemberAttribute]
        public IDictionary<string, string> RequestParam { get; set; }
    }
    /// <summary>
    /// 請求數據包
    /// </summary>
    [DataContractAttribute]
    public class RequestDataSegment
    {
        public RequestDataSegment()
        {
            this.RequestData = new List<RequestData>();
        }
        [DataMemberAttribute]
        public List<RequestData> RequestData { get; set; }
    }
    #endregion

再來看一下響應對象api

#region 響應對象
    /// <summary>
    /// 數據包實體
    /// </summary>
    [DataContractAttribute]
    public class ResponseData
    {
        /// <summary>
        /// 本次傳輸過程當中惟一標識
        /// </summary>
        [DataMemberAttribute]
        public string GuidKey { get; set; }
        /// <summary>
        /// 狀態:100失敗,200成功
        /// </summary>
        [DataMemberAttribute]
        public int Status { get; set; }
        /// <summary>
        /// 數據包:Json對象
        /// </summary>
        [DataMemberAttribute]
        public string Data { get; set; }
    }
    /// <summary>
    /// 響應數據包
    /// </summary>
    [DataContractAttribute]
    public class ResponseDataSegment
    {
        public ResponseDataSegment()
        {
            this.ResponseData = new List<ResponseData>();
        }
        [DataMemberAttribute]
        public List<ResponseData> ResponseData { get; set; }
    }
    #endregion

而咱們服務器對客戶端開放的是一個大接口,或者叫入口接口,它負責把客戶端傳來的請求進行解析,而後代理客戶端,處理多請求,並將結果進行組裝,返回給客戶端,在mvc和web api裏,咱們爲了讓程序擴展性更強,一般把這個核心邏輯寫在attribute裏服務器

下面看一下代碼的實現 mvc

    /// <summary>
    /// Api代理過濾器(api多任務請求的入口)
    /// </summary>
    [AttributeUsage(AttributeTargets.Method)]
    public class ApiProxyFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var Request = filterContext.HttpContext.Request;
            var responseDataSegment = new ResponseDataSegment();
            var data = VCommons.SerializeMemoryHelper.DeserializeFromJson<RequestDataSegment>(Request.Form["dataSeg"]);
            if (data != null && data.RequestData.Any())
            {
                foreach (var item in data.RequestData)
                {
                    try
                    {
                        HttpResponseMessage response;
                        var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
                        using (var http = new HttpClient(handler))
                        {

                            if (item.HttpMethod == 0)
                            {
                                if (item.RequestParam != null)
                                {
                                    item.Url += "?";
                                    foreach (var p in item.RequestParam)
                                        item.Url += p.Key + "=" + p.Value + "&";
                                    item.Url = item.Url.Remove(item.Url.Length - 1, 1);
                                }
                                response = http.GetAsync(item.Url).Result;
                            }
                            else
                            {
                                var content = new FormUrlEncodedContent(item.RequestParam);
                                response = http.PostAsync(item.Url, content).Result;
                            }

                            response.EnsureSuccessStatusCode();
                            responseDataSegment.ResponseData.Add(new ResponseData
                            {
                                GuidKey = item.GuidKey,
                                Status = 200,
                                Data = response.Content.ReadAsStringAsync().Result
                            });
                        }
                    }
                    catch (Exception ex)
                    {

                        responseDataSegment.ResponseData.Add(new ResponseData
                        {
                            GuidKey = item.GuidKey,
                            Status = 100,
                            Data = ex.Message
                        });
                    }

                }

            }
            filterContext.HttpContext.Response.ContentType = "applicatin/json";
            filterContext.HttpContext.Response.Write(VCommons.SerializeMemoryHelper.SerializeToJson(responseDataSegment));
            base.OnActionExecuting(filterContext);
        }
    }

對於你的具體項目,選個主入口,在它上面添加上ApiProxy特性便可app

     /// <summary>
        /// Api統一處理的入口
        /// </summary>
        /// <returns></returns>
        [ApiProxyFilter]
        public JsonResult Index()
        {
            return null;
        }

如今你就能夠去測試你的客戶端了,哈哈,看是否把你的單個請求裏的(三個請求)轉發併爲你返回了,呵呵.ide

回到目錄post

相關文章
相關標籤/搜索