中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

Lind.DDD.Utils.HttpHelper里靜態對象(xiang)引出(chu)的Http超時問題

回到目錄

Lind.DDD.Utils.HttpHelper組件主要實(shi)現(xian)(xian)了(le)對HTTP的各種操(cao)作,如Get,Post,Put和Delete,它(ta)屬于最純粹的操(cao)作,大叔把它(ta)封裝(zhuang)的目的主要為(wei)(wei)了(le)實(shi)現(xian)(xian)與(yu)API安全授權的統(tong)一,你不可(ke)能為(wei)(wei)每(mei)個請求都寫一個“邏輯完全一樣的加密規則”,這(zhe)是(shi)違背DRY原則的,我們(men)應(ying)該通(tong)過面向對象的各位(wei)原則,將(jiang)這(zhe)種可(ke)變的部(bu)分封裝(zhuang)!

公開的統一方法

真正的對象轉鍵/值對

支持對復雜類型,集合(he)類型轉為(wei)Dictionary的(de)鍵值對,它并(bing)不(bu)是網上說(shuo)的(de),只(zhi)把一層屬性進行拼接,而是大叔利用遞歸寫了一個算法(fa),琢層查找對象(xiang)。

      /// <summary>
        /// 將對象(xiang)轉為鍵值對象(xiang)(完全支持(chi)最復雜的類型(xing))
        /// 作者:倉儲大叔
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static IDictionary<string, string> ToDictionary(this object obj)
        {
            try
            {
                var dic = new Dictionary<string, string>();
                var prefix = new Dictionary<string, string>();
                foreach (var p in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    ReGenerate(obj, p, prefix, dic, null);
                    prefix.Clear();
                }
                return dic;
            }
            catch (Exception)
            {

                throw;
            }

        }

其(qi)中(zhong)ReGenerate核(he)心方法被封裝到了Lind.DDD.Utils.HttpHelper組件里

靜態對象引起的超時

對(dui)于Http方(fang)法來說(shuo),我們可以定義(yi)它的(de)(de)(de)handler,添加(jia)一(yi)些壓縮,代(dai)理(li),身(shen)份驗證等(deng)信(xin)息,但在(zai)組件設(she)計(ji)時一(yi)定要(yao)注意,當(dang)你定義(yi)了handler之后(hou),如果又顯示的(de)(de)(de)設(she)計(ji)了超時時間(jian)(jian),千(qian)萬不要(yao)將handler做成全局靜態屬性,因為這樣會讓你的(de)(de)(de)第一(yi)次(ci)請求后(hou)的(de)(de)(de)其它請求都超時,因為你的(de)(de)(de)超時時間(jian)(jian)依賴(lai)你全局的(de)(de)(de)handler,正(zheng)確(que)的(de)(de)(de)做法,應(ying)該在(zai)每個(ge)方(fang)法里(get,post,put,delete)定義(yi)自已(yi)的(de)(de)(de)handler,類似這樣的(de)(de)(de)代(dai)碼(ma)是正(zheng)確(que)的(de)(de)(de)。

        public static HttpResponseMessage Get(string requestUri, NameValueCollection nv = null, int timeOut = 10)
        {
            var handler = new HttpClientHandler() { AutomaticDecompression = System.Net.DecompressionMethods.GZip };
            using (var http = new HttpClient(handler))
            {    //超(chao)時(shi)
                http.Timeout = new TimeSpan(0, 0, timeOut);
                HttpResponseMessage response;
                try
                {
                    response = http.GetAsync(GeneratorUri(requestUri, ApiValidateHelper.GenerateCipherText(nv))).Result;
                }
                catch (Exception ex)
                {
                    response = new HttpResponseMessage(System.Net.HttpStatusCode.RequestTimeout) { Content = new StringContent("請求超時") };
                    Logger.LoggerFactory.Instance.Logger_Error(ex);
                }

                return response;
            }
        }

對于一(yi)種(zhong)知(zhi)識,一(yi)個概念(nian)的(de)理(li)解程度(du),有(you)時(shi)決定(ding)了組件(jian)設(she)計的(de)正確性與安全性!

讓我(wo)們一(yi)起對技(ji)術做更深入,更直接的研究吧!

回到目錄

posted @ 2016-09-02 13:55  張占嶺  閱讀(1063)  評論(0)    收藏  舉報