4

我们使用以下帖子在我们的服务堆栈 API 上启用压缩。

启用 gzip/deflate 压缩

我们的 AppHost 文件中有以下代码:

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

在我的 ApiServiceRunner 中,我有以下内容:

public override object OnAfterExecute(IRequestContext requestContext, object response)
{
    // if it's not null and not already compressed
    if ((response != null) && !(response is CompressedResult))

    // ToOptimizedResult already picks the most optimal compression (hence the name)
    response = requestContext.ToOptimizedResult(response);

    return base.OnAfterExecute(requestContext, response);
}

问题是这段代码现在运行在每个响应上,我们有一个端点,它只从服务器文件系统调用一个 json 文件。当代码在此 json 文件上运行时,它会完全杀死服务器上的应用程序池,并且在调试调用此 json 文件的集成测试时,我看到堆栈溢出异常。

所以我们不得不将以下代码添加到我们的 AppHost 文件中:

public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext)
{
    bool useCustomRunner = actionContext.RequestType.Name != "HomepageLayoutConfigRequest";

    return useCustomRunner 
        ? new ApiServiceRunner<TRequest>(this, actionContext)
        : base.CreateServiceRunner<TRequest>(actionContext);
}

如您所见,当请求类型名称为 HomepageLayoutConfigRequest 时,我们不使用自定义的 ApiServiceRunner。这很丑陋,我们想要一种更好的方法来做到这一点。

有任何想法吗?

谢谢RuSs

附言。这是我最新的 AppHost CreateServiceRunner 覆盖:

    public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext)
    {
        var requestType = actionContext.RequestType;
        string message  = "The [EnableCompression] attribute exists: {0}";

        Debug.WriteLine(string.Format("The requestType was {0}", requestType));

        var useCustomRunner = requestType.HasAttribute<EnableCompression>();
        Debug.WriteLine(string.Format(message, requestType.HasAttribute<EnableCompression>()));

        #region for serviceType if we ever need it. Currently it doesnt work as the guys at SS say it should
        // https://stackoverflow.com/questions/19127522/service-stack-enable-compression-globally
        // Commented out at there is nothing in the EndpointHost.Metadata so getting a null exception - we only need to use the attribute on the request DTO anyway.

        // @Mythz - the following code is the code that doesnt work as per my comments
        //var serviceType = EndpointHost.Metadata.GetServiceTypeByRequest(requestType);

        // @Mythz- this (serviceType) is always null. It is available in next iteration of debugging (1 iteration behind)
        //if (serviceType != null && !useCustomRunner)
        //{
        //    Debug.WriteLine(string.Format("The serviceType was {0}", serviceType));
        //    useCustomRunner = serviceType.HasAttribute<EnableCompression>();
        //    Debug.WriteLine(string.Format(message, serviceType.HasAttribute<EnableCompression>()));
        //}
        #endregion

        return useCustomRunner
            ? new ApiServiceRunner<TRequest>(this, actionContext)
            : base.CreateServiceRunner<TRequest>(actionContext);
    }
4

1 回答 1

2

我认为您走在正确的轨道上,但我更愿意使用自定义属性,例如仅对标有 的服务类或请求 DTO 启用压缩[EnableCompression],您可以执行以下操作:

var serviceType = actionContext.ServiceType;
var requestType = actionContext.RequestType;

var useCustomRunner = serviceType.HasAttribute<EnableCompressionAttribute>()
                   || requestType.HasAttribute<EnableCompressionAttribute>()

return useCustomRunner
    ? new ApiServiceRunner<TRequest>(this, actionContext)
    : base.CreateServiceRunner<TRequest>(actionContext);

我个人喜欢声明式的意图,[EnableCompression][UseCustomRunner]如果你的 ApiServiceRunner 最终做的不仅仅是压缩,你也可以使用类似的东西。

于 2013-10-01T23:32:36.650 回答