Blaž Miheljak - 355 - Feb 3, 2015


I need to log validation errors, with complete request DTO.

I’ve setup ValidationFeature with ErrorResponseFilter in which I do the logging, which works just fine.
The problem is, that I can’t find the way to get hold of the current IRequest in my ErrorResponseFilter.
I tried HttpContext.Current.ToRequest();, but this “recreates” the request, without resolving dto and of course with empty IRequest.Items (which I also need to get some info from previous filters).

Is there a way to get current IRequest instance? 

Thanks, Blaž

Mike Mertsock:

I didn’t see an obvious way to do it, so maybe there’s a better way, but here’s what we do (assumes ServiceStack is hosted in ASP.NET application):

    GlobalRequestFilters.Add((request, res, dto) => {
        HttpContext.Current.Items[“CurrentIRequest”] = request;
        HttpContext.Current.Items[“CurrentDTO”] = dto;

Then in your validation filter you can access HttpContext.Current.Items["…"] to retrieve these objects.

Yep using the RequestContext is the only realy way since the validators are resolved directly from the IOC which doesn’t have access to the current Request.

Blaž Miheljak:

Thank you both!

Actually it looks like we already inject the current request context for validators that implement IRequiresRequest:

This would be the preferred approach.