Enable gzip/deflate compression

北城以北 提交于 2019-11-27 18:31:37

问题


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.


回答1:


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



回答2:


For those interested, a partial answer to my own question, you can use the extension method ToOptimizedResult() or, if you are using caching ToOptimizedResultUsingCache().

For instance, returning a compressed result:

public class ArticleService : Service
{
  public object Get(Articles request) {
    return base.RequestContext.ToOptimizedResult( 
       new List<Articles> { 
            new Article {Ref = "SILVER01", Description = "Silver watch"},
            new Article {Ref = "GOLD1547", Description = "Gold Bracelet"}
       });
  }
}

References

  • CachedServices.cs example
  • CompressedResult.cs
  • Google Group question on Compression in ServiceStack


来源:https://stackoverflow.com/questions/16484763/enable-gzip-deflate-compression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!