启用gzip/deflate压缩 [英] Enable gzip/deflate compression

查看:116
本文介绍了启用gzip/deflate压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 ServiceStack (版本3.9.44.0)用作Windows服务(因此,我使用IIS),并且我将其功能同时用作API和用于提供网页.

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.

我想象如果客户端的请求中包含Accept-Encoding:gzip,deflate标头,则ServiceStack将透明地压缩数据,但是在返回的响应中我看不到任何相应的Content-Encoding:gzip.

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. 在将ServiceStack用作独立服务(不带IIS)的情况下,如何在浏览器接受响应时为响应启用压缩.

  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.

在C#客户端的上下文中,类似地我如何确保客户端/服务器之间的通信被压缩.

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.

谢谢.

推荐答案

如果要在整个API中全局启用压缩,另一种方法是:

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

将此替代添加到您的AppHost:

Add this override to your AppHost:

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

然后像这样实现该类:

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将被调用,使您有机会更改响应.在这里,我正在压缩不为null且尚未压缩的任何内容(以防我在某处使用ToOptimizedResultUsingCache).如果需要的话,您可以更具选择性,但就我而言,我是所有带有json的POCO对象.

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.

这篇关于启用gzip/deflate压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆