次の投稿を使用して、Service Stack 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 ファイルを呼び出すエンドポイントが 1 つあることです。コードがこの 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
ps。ここに私の最新の 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);
}