<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      用JSON做數據傳輸格式中的一些問題總結

      Json 憑借其自身的優勢,在Web數據處理方面已經占據了一定的位置,這段時間涉及到用Json做為數據傳輸格式的項目有3個,其中有部分頁面就采用了Json 數據傳輸格式, 這里我總結下這段時間采用這種方式的一些問題總結,

      向客戶端提供JSON數據的方式

      一. 用WCF提供Json數據

      用WCF向客戶端提供Json數據我們需要注意,

      A. 契約的定義, 在WebInvokeAttribute 或者 WebGetAttribute中的ResponseFormat設置為WebMessageForm.Json,

       

       [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
       
      [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "IsExistSSID/{SSID}", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]

       

      B. EndPointBehavior使用WebHttp

              <behavior name="UIAjaxEndpointBehavior">
                <webHttp />
                <PolicyEndPointBehavior />
              </behavior>

      C. Binding 方式使用webHttpBinding

            <service name="XX.DeviceUIService" behaviorConfiguration="UIAjaxServiceBehavior">
              <endpoint address="" behaviorConfiguration="UIAjaxEndpointBehavior"
                binding="webHttpBinding" contract="DeviceUIServiceContract" />
            </service>

       

      二. 用.Net MVC Action提供 JSON 數據

      1. 在ValueProviderFactories.Factories.Add(new JsonValueProviderFactory())中加入 Json 數據的處理, MVC 3默認是加入的, 如果你使用的是 MVC3, 則無需理會這一點.

       

      2. 采用JsonResult作為你Action的返回值。

      3.返回是使用return Json(XXX); XXX為你要返回的數據,其數據類型必須為可序列化類型.

       

      三. 可采用以asmx為后綴名的簡單WebService來實現,

      四. 使用HttpHandler機制來實現.

       

      因為WCF已被微軟定義為微軟系下的通信平臺,而后兩種隨可以實現,但是是較早的實現方式,所以在此我使用了WCF,直接把所提供的數據,視作系統的數據提供接口.

      而在.NET MVC的環境里, 已經直接支持輸出 Json 形式的數據,所以在非.NET MVC的環境選擇WCF提供, 而在.NET MVC環境直接選擇用JSON Action支持.

       

      WEB客戶端處理

      用JQuery Ajax處理

      把 dataType設置為 'json' 格式,在接收數據時會自動把result轉換為json object格式.

                      $.ajax({
                          url: ‘urladdress’
                          type: 'GET',
                          contentType: 'application/json',
                          dataType: 'json',
                          cache: false,
                          async: false,
                          error: JQueryAjaxErrorHandler,
                          success: function (result) { }
                      });

      異常處理的考慮

      在這里我主要考慮在Web環境下異常的處理, 根據HTTP協議的定義, 每次請求都會返回一個 HTTP Status Code , 不同的Code代表了不同的意義。因此我們的Web應用程序也應該是這樣,根據不同的結果返回不同的 HTTP Status Code , 比如200,代表服務端正確的返回,417代表我們期望的服務端異常,404,請求不存在等, 以及301我們的未授權。

      在WCF環境下,我們首先要給每個方法添加 FaultContract, 如下:

      FaultContract(typeof(WebFaultException<WebErrorDetail>))

      其次我們要對異常做一些處理,讓服務端能返回正確的HTTP Status Code.
                  try
                  {
                       //BussinessCode.....
                  }
                  catch (DuplicateException ex)
                  {
                      throw new WebFaultJsonFormatException<WebErrorDetail>(new WebErrorDetail(ex.Message, ex), HttpStatusCode.ExpectationFailed);
                  }
                  catch (NotExistException ex)
                  {
                      throw new WebFaultJsonFormatException<WebErrorDetail>(new WebErrorDetail(ex.Message, ex), HttpStatusCode.ExpectationFailed);
                  }
                  catch (AppException ex)
                  {
                      throw new WebFaultJsonFormatException<WebErrorDetail>(new WebErrorDetail(ex.Message, ex), HttpStatusCode.ExpectationFailed);
                  }
                  catch (Exception ex)
                  {
                      throw new WebFaultJsonFormatException<WebErrorDetail>(new WebErrorDetail(ex.Message, ex), HttpStatusCode.ExpectationFailed);
                  }


      其中WebFaultJsonFormatException的簽名如下:

          [Serializable, DataContract]
          public class WebFaultJsonFormatException<T> : WebFaultException<T>
          {
              public WebFaultJsonFormatException(T detail, HttpStatusCode statusCode)
                  : base(detail, statusCode)
              {
                  ErrorDetailTypeValidator(detail);
              }
              public WebFaultJsonFormatException(T detail, HttpStatusCode statusCode, IEnumerable<Type> knownTypes)
                  : base(detail, statusCode, knownTypes)
              {
                  ErrorDetailTypeValidator(detail);
              }
      
              private void ErrorDetailTypeValidator(T detail)
              {
                  foreach (DataContractAttribute item in detail.GetType().GetCustomAttributes(typeof(DataContractAttribute), true))
                  {
                      if (item.IsReference)
                          throw new WebFaultJsonFormatException<PureWebErrorDetail>(new PureWebErrorDetail("The DataContractAttribute property 'IsReference' which applied on {0} can't be true when the transfer code type is JSON fromat.", typeof(T).FullName), HttpStatusCode.ExpectationFailed);
      
                  }
              }
      
          }
      
          [Serializable, DataContract(IsReference = false)]
          public class PureWebErrorDetail
          {
              public PureWebErrorDetail(string message, params object[] args)
              {
                  this.Message = string.Format(message, args);
              }
      
              [DataMemberAttribute]
              public string Message { get; set; }
          }
      



      因為我們在JSON做數據傳輸的時候, DataContract中的IsReference是不可以為true的,其意思是相對于XML來說的,XML是可以支持數據的循環引用, 而JSON是不支持的,所以WebFaultJsonFormatException的作用就在于判斷當前我們的JSON數據類型的DataContract的IsReference是否為true, 如果是,則返回一個我們定義好的錯誤信息. 如果沒有采用這個定義,JQUery Ajax因此問題接收到的 HTTP Status Code 是15???的一個錯誤代碼, 但這個錯誤代碼并不是我們正常的 HTTP Status Code 范圍.

       

      異常處理的一個誤區

      最早的時候,由于沒想到用這個方式處理,也是長久寫代碼犯下的一個弊病, 給每個方法加了一個固定的泛型返回值類型

          [DataContract]
          public class TmResult
          {
              [DataMember]
              public bool Success { get; set; }
      
              [DataMember]
              public string ErrorMessage { get; set; }
      
              [DataMember]
              public string FullMessage { get; set; }
      
              [DataMember]
              public string CallStack { get; set; }
          }
      
          [DataContract]
          public class TmResult<T> : TmResult
              where T : class
          {
              [DataMember]
              public T Model { get; set; }
          }


      每次返回都會有一個Success代表是否成功, ErrorMessage代表錯誤情況下的錯誤信息, 這樣做的方式其實就是每次返回的 HTTP Status Code 都是200, 后來知道想到上面的解決辦法之后,才覺得我們更本不需要搞的這么復雜,既然是Web, 那干嗎不把程序寫的更符合HTTP協議的定義, 那樣豈不更簡單。

       

      所以在此也體會到各種標準的好處, 熟悉標準,熟悉編程模型及各種API, 我們的開發會更簡單,更輕松.

      posted @ 2011-12-21 10:56  DukeCheng  閱讀(15771)  評論(14)    收藏  舉報
      主站蜘蛛池模板: 亚洲欧美日韩愉拍自拍美利坚| 嵊泗县| 国产av一区二区麻豆熟女| 墨脱县| 国产亚洲情侣一区二区无| 欧美高清狂热视频60一70| 欧美孕妇乳喷奶水在线观看| 中文字幕结果国产精品| 国产女同疯狂作爱系列| 黑人av无码一区| 亚洲中文字幕无码专区| 久久精品国产高潮国产夫妻| 中文字幕在线精品国产| 亚洲成人资源在线观看| 久久se精品一区精品二区国产| 激情人妻中出中文字幕一区| 久草热大美女黄色片免费看| 四虎www永久在线精品| 亚洲男人av天堂久久资源| 亚洲国产精品综合久久20| 欧洲免费一区二区三区视频| 日韩精品专区在线影院重磅| 91亚洲国产三上悠亚在线播放| 久久99久国产精品66| 国产精品视频一区二区噜噜| 成人无码潮喷在线观看| 一出一进一爽一粗一大视频| 免费看女人与善牲交| 亚洲欧洲国产综合aⅴ无码| 蜜臀av久久国产午夜福利软件| 国产精品毛片在线完整版| 国产成人8X人网站视频| 精品一区二区不卡免费| 中文字幕免费不卡二区| 九九热视频在线免费观看| 亚洲成a人无码av波多野| 成年女人免费视频播放体验区 | 欧美videosdesexo吹潮| 狠狠综合久久综合88亚洲| 东方av四虎在线观看| 18禁裸乳无遮挡啪啪无码免费|