amit_g
amit_g

Reputation: 31270

WCF REST Service Update Header

Using RequestInterceptor one can extract the HTTP headers from request and do some processing on them. One can also update response. However is there a way to update and/or insert HTTP headers in the request itself so that the subsequent processors (e.g. interceptors, authorization managers)?

Upvotes: 0

Views: 320

Answers (2)

Darrel Miller
Darrel Miller

Reputation: 142222

Have you looked at http://wcf.codeplex.com the new HTTP stack has a pipelining model that allows you to do all kinds of things like this.

Upvotes: 1

Greg Sansom
Greg Sansom

Reputation: 20850

WCF has a lot of extension points for doing things like this. What you are probably after is a custom behavior which implements IDispatchMessageInspector.

Create a class which looks like this:

public class MyCustomBehavior : IDispatchMessageInspector, IEndpointBehavior
{
    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        //here you can work with request.Headers.
        return null;
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
    }

    //there are a bunch of other methods needed
    //but you can leave their implementations empty.
    //...
}

You can then add your custom behavior to the service endpoint programatically before you open the service:

host.Description.Endpoints[0].Behaviors.Add(new WcfService2.MyCustomBehavior());

Paolo Pialorsi has a good tutorial which deals with writing message inspectors.

Upvotes: 1

Related Questions