RequestFilter and overrides

Lets say I have a custom MyFilter : RequestFilterAttribute that takes some constants. Like this:

[MyFilter("something")]

And it is configured on a service operation like this:

public class MyService : Service
{
    [MyFilter("something")]
    public MyResponse Get(MyRequest requestDto)
    {
         ... do useful stuff here with requestDto
    }

    ... lots of other service operations

        public bool SomethingSpecial(object requestDto)
        {
        ...do something useful with requestDto
        }
}

And lets say that when the custom filter is executed it does something useful using the IRequest before the service operation receives the actual request. This is all standard ServiceStack stuff to this point.

Now, let say that I have this filter attribute set on a bunch of service operations in my services, but there is one service operation in one service where I want to extend what the filter is doing.

So, when the request comes in, I want the filter to run, and then to delegate what it does to some custom code (SomethingSpecial())that is defined in my service, but only for that specific service operation, and before the service operation is called.

Is that possible? And how would it be setup?

You can check which Service the Request Filter attribute is for my checking the requestDto, e.g:

public override void Execute(IRequest req, IResponse res, object requestDto)
{
    var myRequest = requestDto as MyRequest;
    if (myRequest != null)
    {
        //...
    } 
}

How do I de-reference the specific service and service operation from the requestDto?

I’m hoping you don’t plan on calling it manually in a filter because then it will be called twice.

But you can get the Service class with:

var dtoType = requestDto.GetType();
var service = HostContext.Metadata.GetServiceTypeByRequest(dtoType);

To get the method you’ll need to use Reflection, something like:

var mi = service.GetMethods().FirstOrDefault(x => x.Name.EqualsIgnoreCase(req.Verb)
    && x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType == dtoType)
  ?? service.GetMethods().FirstOrDefault(x => x.Name.EqualsIgnoreCase(ActionContext.AnyAction)
    && x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType == dtoType);