Page 1 of 1

ServiceStack Request and Response Filters

PostPosted: Fri Jun 22, 2018 4:15 pm
by SBarnes
Hi Mike,

Is there a way with ServiceStack to make a request and/or response filter fire ahead of everything else, I am looking at the concept of a licensing model for a possible client that I would want to control from the server based upon certain criteria?

Re: ServiceStack Request and Response Filters

PostPosted: Sat Jun 23, 2018 2:27 pm
by Mike.Sheen
Hi Stuart,

After looking at the Order of Operations for ServiceStack, adding a request filter with a Priority of < 0, then that would be invoked before our built-in global request filter.

So, my understanding (and I've not proven this!) is that if you create a class which inherits from ServiceStack's RequestFilterAttribute, and set the Priority property to something < 0 - then any service classes decorated with an attribute of your custom class will cause that service to execute your custom request filter before our global request filter.

But - that would mean you'd have to decorate every service with the attribute - if you are wanting to apply this to our built-in services then that's not ideal - but if it's for your own custom service classes you've added, then it's not a problem.

If you do need a way of executing your custom request filter for our built-in classes before our global filter (without adding the attribute to our classes), then we'd have to build in some functionality to make that possible. That should be fairly simple - we just expose a list of delegate functions you can add to, and our global filter invokes those in order before anything else goes on in our global request filter.

There are also prerequest filters in ServiceStack - but those get invoked before the DTO is deserialised, and before our global request filter - that may or may not suit your needs.

I've added DEV-6678 to look into plugins adding request filters which are executed before our global filters.

Mike

Re: ServiceStack Request and Response Filters

PostPosted: Sat Jun 23, 2018 7:01 pm
by SBarnes
Thanks Mike,

I had read the order of operations stuff but hadn't gotten much further than that.

But I like the idea of what you are suggesting which should work for what I need.

Re: ServiceStack Request and Response Filters  Topic is solved

PostPosted: Mon Jun 25, 2018 1:02 pm
by Mike.Sheen
Hi Stuart,

Can you take a look at the attached and let me know if it suits your needs? It's simply a plugin (should work with any version 7.1.0 or later) which adds a global request filter to the start of the GlobalRequestFilters of ServiceStack's appHost. In this example we add a request filter looking for a request type of CarrierGETRequest (defined in our standard REST API Plugin) - and if the type matches it throws an exception - this is executed before the global filters defined in the REST API plugin.

I didn't need to modify our standard REST API plugin, or anything else - just a simple .Insert into the GlobalRequestFilters list at index 0 was what was needed. The only trick was dealing with helping fusion resolve any needed assemblies if the filter provided refers to any types defined in another plugin.

Mike

Re: ServiceStack Request and Response Filters

PostPosted: Mon Jun 25, 2018 1:07 pm
by SBarnes
Thanks Mike,

I'll take a look and come back to you on this when I can.

Re: ServiceStack Request and Response Filters

PostPosted: Tue Jun 26, 2018 3:21 pm
by SBarnes
Hi Mike,

It looks like it will do what I want, just one further question if I add a header to the response do you know how to read that out of the JsonServiceClient?

Re: ServiceStack Request and Response Filters

PostPosted: Tue Jun 26, 2018 5:33 pm
by Mike.Sheen
Hi Stuart,

The ServiceStack JsonServiceClient has a ResponseFilter and GlobalResponseFilter property you can use to examine the response (including headers) before it's deserialised.

Code: Select all
string myHeader = null;
var client = new JsonServiceClient(baseUrl) {
    ResponseFilter = res => myHeader = res.Headers["X-MyHeader"],
};

var response = client.Post(request);


With the above, once the client.Post call returns, the response variable will contain the deserialised DTO, and the myHeader variable will contain the contents of the "X-MyHeader" response header.

Mike