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)?
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.
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.
精彩评论