【发布时间】:2011-01-21 22:12:46
【问题描述】:
使用 RequestInterceptor 可以从请求中提取 HTTP 标头并对其进行一些处理。还可以更新响应。但是,有没有办法在请求本身中更新和/或插入 HTTP 标头,以便后续处理器(例如拦截器、授权管理器)?
【问题讨论】:
使用 RequestInterceptor 可以从请求中提取 HTTP 标头并对其进行一些处理。还可以更新响应。但是,有没有办法在请求本身中更新和/或插入 HTTP 标头,以便后续处理器(例如拦截器、授权管理器)?
【问题讨论】:
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,负责编写消息检查器。
【讨论】:
Headers 是 get-only,但您仍然可以调用 Headers.Add(...) 来修改集合。
你看过http://wcf.codeplex.com吗?新的HTTP堆栈有一个流水线模型,可以让你做各种各样的事情。
【讨论】: