Minor issue with MessageHandler and WebApiConfiguration

Topics: Web Api
Oct 15, 2011 at 10:46 PM

Preview 5:

When MessageHandlers are added to the WebAPiconfiguration object they are intercept the request in the reverse order.

e.g. var config = new WebApiconfiguration();

config.MessageHandlers.Add(new AuthenticationMessageHandler());

config.MessageHandlers.Add(new AuthorisationMessageHandler());

the sequence it intercept the request is AuthorisationMessageHandler and then AuthenticationMessageHandler.

It would be nice they intercept the request in the order they are added to the config.

 

-Henry

Oct 15, 2011 at 11:36 PM

Hi,

Actually this is an expected behavior:

The handlers are invoked in a bottom-up fashion in the incoming path and top-down in the outgoing path. That is, the last entry is called first for an incoming request messasge but invoked last for an outgoing response message.

If it makes easy, in your example, you can think of them like one handler is stacked over the other:

AuthenticationMessageHandler

AuthorisationMessageHandler

Request:     -> AuthorisationMessageHandler -> AuthenticationMessageHandler ->

Response:  <- AuthorisationMessageHandler <- AuthenticationMessageHandler <-

Thanks,Kiran Challa

Oct 16, 2011 at 2:39 AM

So does that mean on the way out the same handlers would be called again??

If not how do I distinguish between request message handlers and response message Handlers. 

Coordinator
Oct 16, 2011 at 2:41 AM
Please file an issue in the Issue Tracker and we will take a look at what can be done here.

Sent from my Windows Phone

From: HenryBhavsar
Sent: 10/15/2011 3:50 PM
To: Daniel Roth
Subject: Minor issue with MessageHandler and WebApiConfiguration [wcf:276025]

From: HenryBhavsar

Preview 5:

When MessageHandlers are added to the WebAPiconfiguration object they are intercept the request in the reverse order.

e.g. var config = new WebApiconfiguration();

config.MessageHandlers.Add(new AuthenticationMessageHandler());

config.MessageHandlers.Add(new AuthorisationMessageHandler());

the sequence it intercept the request is AuthorisationMessageHandler and then AuthenticationMessageHandler.

It would be nice they intercept the request in the order they are added to the config.

-Henry

Oct 16, 2011 at 3:24 AM
henrybhavsar wrote:

So does that mean on the way out the same handlers would be called again??

If not how do I distinguish between request message handlers and response message Handlers. 

Yeah...right...the same handlers would be executed on the request and the response...BUT, you are provided the ability to do Pre or Post processing or Both...Example below (we depend on the System.Threading.Tasks API):

public class ResponseContentModificationHandler : DelegatingHandler
    {
        protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
// modify incoming request here
request.Headers.Add(“h1”,”h1-value”);                

            return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>((responseToCompleteTask) =>
            {
                HttpResponseMessage response = responseToCompleteTask.Result;

                // modify outgoing response here
                response.Headers.Add(“h2”, “h2-value”);   

                return response;
            });
        }
    }

As you can see, you can opt-in to do Pre or Post or Both processing on messages.

hope this helps...

Thanks,Kiran Challa

Oct 16, 2011 at 4:31 AM

Thanks Kiran. I will try out...and might ask you further question later if I stumble upon. Appreciate your prompt response.