This project is read-only.

Help with DelegatingHandler vs HttpOperationHandler

Topics: Web Api
Oct 25, 2011 at 5:30 PM

Would really appreciate some help on this one :)

I'm still a bit confused on when to use DelegatingHandler and when to use HttpOperationHandler, but the following blog post helped a bit :)

I'm creating a handler that extracts information from a custom X- HTTP header and needs to put it in a place accessible from the rest of the code.
My first idea was to use a DelegatingHandler and put the value into the HttpContext, but quickly discovered that doesn't work since the handler often runs on a different thread than the HTTP request.
Then I started looking at HttpOperationHandler but that seems to be more concerned with the actual application and dispatching requests to methods.

I've looked in the source and WCF contrib but haven't found an example that suits my needs.

Oct 25, 2011 at 5:48 PM

you may use Properties collections in HttpRequestMessage into DelegatingHandler and force HttpRequestMessage as input parameter at ServiceContract level

In case you prefer force/inject a specific parameter, not entire HttpRequestMessage, you may consider HttpOperationHandler instead

Oct 25, 2011 at 6:05 PM

I see what you mean, but that would require me to pollute a lot of classes with extra parameters just to pass the data around.  In my case I'm not using this information directly in the operation method itself.

Is there really no thread-safe request context we can use?

Oct 25, 2011 at 10:18 PM

You can still add properties to the request message from your message handler even if you don’t need the request message in the operation. Does that work for you?

Daniel Roth

Oct 25, 2011 at 11:15 PM

Not unless there is a way to access the request message from other classes :)

Oct 26, 2011 at 12:36 AM

Use HttpRequestMessage<T> as a parameter on your operation where T is the body, or just use HttpRequestMessage by itself.


Oct 26, 2011 at 6:06 AM

From where do you require access to this data?

Daniel Roth

Oct 26, 2011 at 11:03 AM

I'll admit my case is rather unusual, and kind of breaks separation of concerns, but I need to access this piece of data from the DAL and from a log4net property.

Oct 26, 2011 at 11:59 AM

I found a solution to this :) I created a HttpOperationHandler that takes the HTTP header from the HttpRequestMessage and puts it into the HttpContext (which is available in op. handlers).  I then registered this as a request handler.

What confused me before was how the operation handlers work, but the signatures of the methods can be quite confusing.