2

In my previous project, I use a framework (Agatha RRSL) similar to ServiceStack, in that everything is made of Request, Response and Handler. It also has Interceptors that can attach to handler and you can inject other interfaces to the handler as well. I can use this to open a transaction BeforeHandling, access to both request and response in AfterHandling, create audit, save to database and close transaction if needed.

I try to experiment similar with SerivceStack. But seems with Filters, I can't grab request and response together?

With custom ServiceRunner. When I try to debug OnAfterExecute(...), I can see the name of my request dto in IRequestContext {ServiceStack.ServiceHost.HttpRequestContext}. But just the name, I couldn't figure out how to retrieve the actual request object to work with the response object.

Another thing I haven't figure out is if it's possible to inject the auto wired service interface into it, like a db context or audit service. Maybe this one is too far ahead in the pipeline?

The final thing is, it seems I can only register one custom service runner? With Interceptor, I can drop a bunch of them, and they will wrap around each other.

Any thoughts? Thanks

Whoever
  • 1,295
  • 1
  • 14
  • 21

1 Answers1

3

The RequestContext also contains the HttpRequest and HttpResponse which you can get access with:

var httpReq = RequestContext.Get<IHttpRequest>();
var httpRes = RequestContext.Get<IHttpResponse>();

See the docs on Accessing HTTP Specific features for more info.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • Thanks. That seems to get a HttpRequest. How do I get the original Request Dto? Ideally from ResponseFilter, rather than service runner. I watched almost everything in the debugger, still couldn't figure out. – Whoever Oct 10 '13 at 03:24
  • @Whoever Response filters act on Responses which is why they're given a Response DTO. If you need to pass metadata throughout the request pipeline, it is recommended to use `Request.Items` instead. Though the Request DTO is accessible with RequestContext.Dto. – mythz Oct 10 '13 at 05:18
  • Still haven't figured out how to access RequestContext.Dto, any hints? I try to access custom auditing attributes on request dto, combined with result in response dto, and generate an auditing object. For example, user xyz logon, success/failed, etc. So, I need the original request Dto in the later half of the pipeline, either in OnAfterExecute or ResponseFilterAttribute. – Whoever Oct 10 '13 at 13:39
  • So what is on `RequestContext.Dto`? And like I said you can pass any data (inc a Request DTO) using `Request.Items` which is accessible throughout the request pipeline. – mythz Oct 10 '13 at 17:14
  • Sorry, missed some cast. Finally was able pass request dto into response filter and get all the custom attributes as well. I guess the only inconvenience is that you have to apply filter in pairs. Speaking of that, if both Request/Response filter are added on top of HelloService as shown in wiki, response filter won't fire. I have to move response filter to object Any(Hello request). Not sure if that's by design, or I miss something. Not a big deal as they probably should be on the dto anyway. I think I have found almost everything I need and ready for some real action! – Whoever Oct 10 '13 at 19:22
  • Some random thoughts, maybe custom service runner can be attached the main runner, rather than replacing it? Then you can register a bunch of them and run in sequence, like the handler I mentioned. Seems a more convenient way to add aspect style code compare to filters. Although I don't know enough to say if that's a better approach, and the potential fall out. Another fun thing in the Agatha is that the service takes an array of requests and return an array of responses. So you are free to batch several different requests in a single round trip, seems fun. Thanks again for the great work! – Whoever Oct 10 '13 at 19:25
  • @Whoever Note: you need to call the base class when you're overriding a ServiceRunner if you want to keep the existing behavior. As for the batching, it would be ideal to come up with a generic solution so you get the functionality for free on existing Services. Best to submit a [feature request](http://servicestack.uservoice.com/forums/176786-feature-requests) for this so we can measure interest and prioritize accordingly. – mythz Oct 10 '13 at 20:24
  • Thanks, settled for custom attributes on request dto plus global filter for now. – Whoever Oct 11 '13 at 01:25