0

I've got compression propperly configured for my Azure web role. Both .aspx pages and static pages like *.css are being compressed correctly.

<urlCompression doStaticCompression="true" doDynamicCompression="true" dynamicCompressionBeforeCache="true" />

I've got several different [System.Web.Services.WebMethod]'s though, that are not returning GZIP'd data. The size of each request is around 350KB, so I'm thinking it should be quite a bit faster if I can get this to work.

Within my webMethod, I create a list of objects, return the objects, and I assume some type of built in serializer turns this into JSON?

Is there anyway to force this content to be compressed? Thanks so much!

kevin
  • 417
  • 4
  • 20
  • Seems like this should answer it: http://stackoverflow.com/questions/2775261/how-to-enable-gzip-http-compression-on-windows-azure-dynamic-content – Oskar Austegard Feb 15 '12 at 21:45

2 Answers2

0

I've seen people have issues with built in Compression for numerous reasons

The simplest way is to use a third party component such as Telerik's RadCompression to enforce compression on the response to AJAX calls.

Alternatively, you can override the application's BeginRequest method or write your own handler to pack up the responses on the fly. A basic VB version of how to do this is here:

Sub Application_BeginRequest(...)

If Request.RawUrl.Contains(".aspx") And _ Not Request.Headers("Accept-Encoding") Is Nothing Then

If Request.Headers("Accept- encoding").ToLower().Contains("gzip") Then

Response.Filter = New GZipStream(Response.Filter,CompressionMode.Compress, True) Response.AppendHeader("Content-encoding", "gzip")

' Else...attempt deflate if GZip is not allowed

End If

End If

End Sub

I've done a method with the handler as well (and that's what I believe Telerik's RadCompression uses), but it is a good bit more complicated as you have to modify the response size, etc.

OperatorOverload
  • 724
  • 4
  • 17
0

Here's what I ended up with, a variation Yak's answer.

        HttpApplication app = (HttpApplication)sender;
        HttpRequest request = app.Request;
        HttpResponse response = app.Response;

        System.Web.HttpApplication Appl = (System.Web.HttpApplication)sender;
        HttpContext context = Appl.Context;
        string origpath = context.Request.Url.AbsolutePath;


        //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.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
                        response.AddHeader("Content-encoding", "gzip");
                    }
                    else if (acceptEncoding.Contains("deflate"))
                    {
                        response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
                        response.AddHeader("Content-encoding", "deflate");
                    }
                }
            }
        }
kevin
  • 417
  • 4
  • 20