WCF REST服务更新标头

时间:2011-01-21 02:15:13

标签: wcf rest

使用RequestInterceptor可以从请求中提取HTTP头并对它们进行一些处理。还可以更新响应。但是有没有办法在请求本身更新和/或插入HTTP头,以便后续的处理器(例如拦截器,授权管理器)?

2 个答案:

答案 0 :(得分:1)

WCF有一个很多的扩展点来做这样的事情。你可能会追求的是一个实现IDispatchMessageInspector的自定义行为。

创建一个如下所示的类:

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.
    //...
}

然后,您可以在打开服务之前以编程方式将自定义行为添加到服务端点:

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

Paolo Pialorsi有一个good tutorial来处理写信息检查员。

答案 1 :(得分:1)

您是否看过http://wcf.codeplex.com新的HTTP堆栈有一个流水线模型,允许您执行此类各种操作。

相关问题