1

応答サイズを小さくするために、gzip を使用して応答を圧縮する IHttpModule を作成しました (大量のデータを返します)。Web サービスが例外をスローしない限り、うまく機能しています。例外がスローされた場合、例外は gzip されますが、Content-encoding ヘッダーは消え、クライアントは例外を読み取ることを知りません。

どうすればこれを解決できますか? ヘッダーがないのはなぜですか?クライアントで例外を取得する必要があります。

モジュールは次のとおりです。

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.");
}

助けていただければ幸いです。

ありがとう!

4

1 に答える 1

1

この質問を投稿してからしばらく経ちましたが、同じ問題が発生しました。修正方法は次のとおりです。

Init()メソッドで、Errorイベントのハンドラーを追加します

app.Error += new EventHandler(app_Error);

ハンドラーで、ResponseヘッダーからContent-Typeを削除し、Response.Filterプロパティをnullに設定します。

void app_Error(object sender, EventArgs e)
{

    HttpApplication httpApp = (HttpApplication)sender;
    HttpContext ctx = HttpContext.Current;

    string encodingValue = httpApp.Response.Headers["Content-Encoding"];

    if (encodingValue == "gzip" || encodingValue == "deflate")
    {
        httpApp.Response.Headers.Remove("Content-Encoding");
        httpApp.Response.Filter = null;
    }

}

たぶん、これを行うためのよりエレガントな方法がありますが、私のためにトリックをしました。

于 2011-07-28T18:00:26.770 に答える