This project is read-only.

Best way to intercept requests to specific operations?

Topics: Web Api
Nov 11, 2011 at 2:47 AM

Hi there,

I've been using the WCF Rest service applications released in .Net 4.0.

I've only just stumbled upon this Web API project today after attempting to solve a problem I'm having.

I really like what I see in the Web API (Specifically it looks to take away all the pain points that we had with the WCF service application around error handling, programatically increasing max message size, replacing outgoing messages with the JSON.Net serializer etc.).

What I want to achieve is this:

A user of my library tags a specific operation with an attribute (I assume this would be an IOperationBehavior), for now lets call this "InterceptMe".

I want the operation to have a specific signature i.e.:


Post(CustomObject cObject)



When a request (I'm specifically interested in POST) comes in to this operation, I want to intercept it, I want to have access to the raw http request body i.e. the request stream.

I want to inspect this request body, if it matches certain criteria I want to extract some info from it, and then using that extracted info, create a NEW CustomObject and pass that to the operation.

If the request does not match certain criteria I need to throw an exception so that the operation never ends up getting invoked.

The point of this is to transparently receive chunked requests from a client, and only call the users operation once I have received and combined all chunks.


I originally tried to do this with an IOperationInvoker but the problem is, inside the invoke method, the request body has already been run through the IDispatchMessageFormatter, which means that the inputs array contains whatever the IDispatchMessageFormatter decided it needs to contain.

i.e. In the above example inputs[0] would contain an already constructed CustomObject

I want access to the request stream regardless of the signature of the operation (As I will manually handle constructing the right arguments to pass to the operation).


I hope this is clear enough, I believe I can achieve what I want to achieve with WCF it's just a complex beast :) (Powerful though).


Nov 11, 2011 at 1:45 PM

What you are describing is exactly what OperationHandlers do.  Glenn has a blog post on them here