If you want to enable compression globally for your API, you need to do the following:
Add this override to AppHost:
public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext) { return new MyServiceRunner<TRequest>(this, actionContext); }
Then we implement this class as follows:
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 will be called and will give you the opportunity to change the answer. Here I am compressing everything that is not null and not compressed (in case I use ToOptimizedResultUsingCache somewhere). You may be more selective if you need to, but in my case, I am all POCO objects with json.
References
source share