【问题标题】:Exception breaks response compression异常中断响应压缩
【发布时间】:2011-01-29 07:21:49
【问题描述】:

我写了一个 IHttpModule 使用 gzip 压缩我的响应(我返回大量数据。)以减少响应大小。只要 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;
        }
    }
}

这里是网络服务:

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

【问题讨论】:

  • 请帮忙?您无需提及,因为这是本网站的实际目的。
  • 这仍然会发生。 (叹气!)显然,这是因为在处理错误时,ASP.NET clears out all current headers

标签: asp.net ajax gzip response


【解决方案1】:

您应该尝试在page_errorapplication_error 事件处理程序中处理异常。 我不知道标头会发生什么,但您可能可以通过在应用程序中除以零来模拟和调试 Compress 方法。

查看link 了解 ASP.NET 中的全局异常处理

【讨论】:

  • 万一我的网络服务抛出异常,我希望客户端在“错误”回调下得到这个异常。如果出现异常,我不想将结果视为成功。
  • 那你为什么不以你的方式处理异常然后重新抛出它
  • 这正是我要做的,但我是在特定的网络服务中这样做的,而不是在 page_error 事件下。
  • 我认为您的问题是由于在 asp.net 的“更深”部分中未捕获的异常冒泡造成的,尝试在 application_error (global.asax) 中捕获它,然后您可能会明白发生了什么。如果不查看代码并对其进行调试,我无法告诉您更多信息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-12
  • 1970-01-01
  • 2012-09-15
  • 2011-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多