Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable gzip/deflate compression

I'm using ServiceStack (version 3.9.44.0) as a Windows Service (so I'm not using IIS) and I use both its abilities both as an API and for serving web pages.

However, I haven't been able to find how exactly I should enable compression when the client supports it.

I imagined that ServiceStack would transparently compress data if the client's request included the Accept-Encoding:gzip,deflate header, but I'm not seeing any corresponding Content-Encoding:gzip in the returned responses.

So I have a couple of related questions:

  1. In the context of using ServiceStack as a standalone service (without IIS), how do I enable compression for the responses when the browser accepts it.

  2. In the context of a C# client, how do similarly I ensure that communication between the client/server is compressed.

If I'm missing something, any help would be welcome.

Thank you.

like image 831
Renaud Bompuis Avatar asked May 10 '13 14:05

Renaud Bompuis


People also ask

Should I Enable gzip compression?

Enabling gzip compression is great for improving page speed because your visitors will need to download much smaller web files as the original ones when browsing your web pages, which speeds up the download process of these files. There's no reason to not use it these days.

What is gzip and DEFLATE?

Gzip is the more reliable because it is deflate plus a few headers and a check sum. In other words gzip is deflate, and extra headers and check sum. Deflate is checked with adler32, which is also part of gzip. Because the gzip payload is a DEFLATE-compressed payload. Deflate info.

What is accept Encoding gzip DEFLATE?

The Accept-Encoding header is used for negotiating content encoding. Accept-Encoding: gzip, deflate. The server responds with the scheme used, indicated by the Content-Encoding response header. Content-Encoding: gzip. Note that the server is not obligated to use any compression method.


1 Answers

If you want to enable compression globally across your API, another option is to do this:

Add this override to your AppHost:

public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext)
{
    return new MyServiceRunner<TRequest>(this, actionContext);
}

Then implement that class like this:

public class MyServiceRunner<TRequest> : ServiceRunner<TRequest>
{
    public MyServiceRunner(IAppHost appHost, ActionContext actionContext) : base(appHost, actionContext)
    {
    }

    public override void OnBeforeExecute(IRequestContext requestContext, TRequest request)
    {
        base.OnBeforeExecute(requestContext, request);
    }

    public override object OnAfterExecute(IRequestContext requestContext, object response)
    {
        if ((response != null) && !(response is CompressedResult))
            response = requestContext.ToOptimizedResult(response);

        return base.OnAfterExecute(requestContext, response);
    }

    public override object HandleException(IRequestContext requestContext, TRequest request, Exception ex)
    {
        return base.HandleException(requestContext, request, ex);
    }
}

OnAfterExecute will be called and give you the chance to change the response. Here, I am compressing anything that is not null and not already compressed (in case I'm using ToOptimizedResultUsingCache somewhere). You can be more selective if you need to but in my case, I'm all POCO objects with json.

References

  • ServiceStack New Api
like image 116
John Avatar answered Oct 27 '22 13:10

John