我们使用以下帖子在我们的服务堆栈 API 上启用压缩。
我们的 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);
}