ServiceStack ResponseFilterAttribute not being cal

2019-05-07 18:43发布

问题:

//---------------------------------------------------------------------
//Aspect Filters
public class RequestAspectAttribute : RequestFilterAttribute {
  public RequestAspectAttribute() { } //debug point was hit
  public RequestAspectAttribute(ApplyTo applyTo) : base(applyTo) { }
  public override void Execute(IHttpRequest req, IHttpResponse res, object reqDto) {
      //This code is executed before the service 
      //debug point was hit
  }
}
public class ResponseAspectAttribute : ResponseFilterAttribute {
  public ResponseAspectAttribute() { } //debug point was NOT hit
  public ResponseAspectAttribute(ApplyTo applyTo) : base(applyTo) { }
  public override void Execute(IHttpRequest req, IHttpResponse res, object resDto) {
      //This code is executed after the service 
      //debug point was NOT hit
  }
}
//---------------------------------------------------------------------
//REST Service
[RequestAspect]
[ResponseAspect]
public class TodoService : RestServiceBase<Todo> { ...

I am testing out the Req/Res Filter Attributes on the ToDo List sample project with the code above. So nothing else has been changed to the sample project (I think) except for the two additional attributes.

When I add a todo item, only the request attribute was called. The response attribute didn't get triggered.

Shouldn't they fire up in a pair before & after a Rest call in this case? Is my understanding incorrect or am I doing something wrong? Thank you ahead for your help.

回答1:

Use your request and response filters with respective Request and Response DTO

    [Route("/Hello")]
    [RequestAspect]
    public class HelloRequest
    {
        public string hello { get; set; }
    }
    [ResponseAspect]
    public class HelloResponse
    {
        public string hello { get; set; }
    }
    public class HelloService : Service
    {
        public object Any(HelloRequest req)
        {
            return new HelloResponse
            {
                hello = req.hello
            };
        }
    }