2010-03-19 106 views
1

我寫了一個IHttpModule,它使用gzip(我返回大量數據)壓縮我的響應,以減少響應大小。只要Web服務不會拋出異常,它就會很好地工作。在引發異常的情況下,該異常被壓縮,但內容編碼頭部消失,並且客戶端不知道讀取異常。請求壓縮

爲什麼標題丟失?我需要在客戶端中獲得例外。

這裏是模塊:

public class JsonCompressionModule : IHttpModule 
{ 
    public JsonCompressionModule() 
    { 
    } 

    public void Dispose() 
    { 
    } 

    public void Init(HttpApplication app) 
    { 
     app.BeginRequest += new EventHandler(Compress); 
    } 

    private void Compress(object sender, EventArgs e) 
    { 
     HttpApplication app = (HttpApplication)sender; 
     HttpRequest request = app.Request; 
     HttpResponse response = app.Response; 
     try 
     { 
      //Ajax Web Service request is always starts with application/json 
      if (request.ContentType.ToLower(CultureInfo.InvariantCulture).StartsWith("application/json")) 
      { 
       //User may be using an older version of IE which does not support compression, so skip those 
       if (!((request.Browser.IsBrowser("IE")) && (request.Browser.MajorVersion <= 6))) 
       { 
        string acceptEncoding = request.Headers["Accept-Encoding"]; 

        if (!string.IsNullOrEmpty(acceptEncoding)) 
        { 
         acceptEncoding = acceptEncoding.ToLower(CultureInfo.InvariantCulture); 

         if (acceptEncoding.Contains("gzip")) 
         { 
          response.AddHeader("Content-encoding", "gzip"); 
          response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); 
         } 
         else if (acceptEncoding.Contains("deflate")) 
         { 
          response.AddHeader("Content-encoding", "deflate"); 
          response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); 
         } 
        } 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      int i = 4; 
     } 
    } 
} 

這裏是Web服務:

[WebMethod] 
public void DoSomething() 
{ 
    throw new Exception("This message get currupted on the client because the client doesn't know it gzipped."); 
} 
+0

請幫忙?您無需提及,因爲這是本網站的實際目的。 – 2011-01-24 22:45:09

回答

0

你應該嘗試應對PAGE_ERROR或Application_Error事件處理程序的例外。 我不知道標題會發生什麼,但是您可能可以通過在應用程序內除以零來模擬和調試Compress方法。

檢查此link關於ASP.NET中的全局異常處理

+0

萬一我的webservice拋出一個異常,我想客戶端會在「error」回調下得到這個異常。 如果發生異常,我不想將結果視爲成功。 – Naor 2010-03-20 03:08:24

+0

那麼你爲什麼不用自己的方式處理異常,然後重新拋出它 – tsinik 2010-03-20 16:09:51

+0

這正是我的東西,但我在特定的Web服務中這樣做,而不是在page_error事件下。 – Naor 2010-03-23 10:40:56