8

What would be the easiest way to setup a request/response interceptor in ServiceStack that would execute for a particular service?

A request filter (IHasRequestFilter) works fine but a response filter (IHasResponseFilter) is not triggered if the service returns non 2xx status code. I need to retrieve the status code returned by the method as well as the response DTO (if any).

A custom ServiceRunner and overriding the OnBeforeExecute and OnAfterExecute methods seems to work fine but I find it pretty intrusive as the service runner need to be replaced for the entire application and I couldn't find a way clean way to isolate per functionality the tasks that need to be executed in those methods.

Is there some extension point in ServiceStack that I am missing that would allow me to execute some code before each service method and after each service method? A plugin would be ideal but how can I subscribe to some fictitious BeforeExecute and AfterExecute methods that would allow me to run some custom code?


UPDATE:

Just after posting the question I found out that global response filters are executed no matter what status code is returned by the service which is exactly what I needed. So one last question: Is it possible to retrieve the service type that will handle the request in a request filter? I need to check whether this service is decorated by some custom marker attribute.

4

3 回答 3

3

我找到了有关如何在自定义请求/响应过滤器中检索服务类型的问题的解决方案:

appHost.RequestFilters.Add((req, res, requestDto) =>
{
    var metadata = EndpointHost.Metadata;
    Type serviceType = metadata.GetServiceTypeByRequest(requestDto.GetType());

    ...
}
于 2013-07-08T16:19:40.260 回答
3

自定义 ServiceRunner 并覆盖 OnBeforeExecute 和 OnAfterExecute 方法似乎工作正常,但我发现它非常具有侵入性,因为需要为整个应用程序替换服务运行器

快速说明,您可以选择加入并仅选择应使用自定义服务运行器的请求,例如:

public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(
  ActionContext actionContext)
{           
    return useCustomRunner(actionContext.RequestType)
      ? new MyServiceRunner<TRequest>(this, actionContext)
      : base.CreateServiceRunner<TRequest>(actionContext);
}
于 2013-07-08T16:30:48.177 回答
2

IHttpRequest 有 OperationName。我想这就是你所追求的。

于 2013-07-08T14:38:48.533 回答