views:

54

answers:

1

Hi, I wrote an IHttpModule that compress my respone using gzip (I return a lot of data) in order to reduce response size. It is working great as long as the web service doesn't throws an exception. In case exception is thrown, the exception gzipped but the Content-encoding header is disappear and the client doesn't know to read the exception.

How can I solve this? Why the header is missing? I need to get the exception in the client.

Here is the module:

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;
        }
    }
}

Here is the web service:

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

I appriciate any help.

Thanks!

A: 

You should try to deal with the exception in page_error or in application_error event handlers. I don't know what happens to the header but you probably can simulate and debug the Compress method by dividing by zero inside your application.

Check this link about global exception handling in ASP.NET

tsinik
In case my webservice throws an exception, I would like the client will get this exception under the "error" callback.In case of exception I don't want to treat the result as a success.
Naor
Then why don't you deal with the exception in your way and then rethrow it
tsinik
That is exactly what I'm dong, but I do that in the specific web service and not under page_error event.
Naor
I think that your problem is due to bubbling of the uncatched exception in "deeper" parts of asp.net, try to catch it in application_error (global.asax) and then you may understand what happend.I can't tell you more without looking at the code and debbuging it
tsinik