gpt4 book ai didi

iis - 服务堆栈启用全局压缩

转载 作者:行者123 更新时间:2023-12-04 14:41:38 25 4
gpt4 key购买 nike

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

Enable gzip/deflate compression .

我们的 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。这很丑陋,我们想要一种更好的方法来做到这一点。

有任何想法吗?

谢谢
俄罗斯

附:这是我最新的 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);
}

最佳答案

我认为您在正确的轨道上,但我更喜欢使用自定义属性,例如,仅对标有 [EnableCompression] 的服务类或请求 DTO 启用压缩, 你可以做:

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 最终做的不仅仅是压缩。

关于iis - 服务堆栈启用全局压缩,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19127522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com