我这里有两个问题:
1)Microsoft.ServiceModel.Web.RequestInterceptor
和System.ServiceModel.Dispatcher.DispatchRuntime.MessageInspectors(IdispatchMessageInterceptor)
之间的基本区别是什么?
两者都似乎是可用于实现请求管道中的自定义验证/拦截器的请求/消息拦截器。
何时使用一个而非另一个?
2)还有如何将RequestInterceptor
插入到RouteTable.Routes.Add(new ServiceRoute())
中
我有这样的一个类 -
public class AuthenticationInterceptor : RequestInterceptor
{
//Authentication logic goes here......
}
一个像下面这样的路由定义:
RouteTable.Routes.Add(new ServiceRoute(routePrefix, new MyServiceHostFactory(container, (sh) => {
foreach (System.ServiceModel.Dispatcher.ChannelDispatcher cd in sh.ChannelDispatchers)
{
foreach (System.ServiceModel.Dispatcher.EndpointDispatcher ed in cd.Endpoints)
{
ed.DispatchRuntime.MessageInspectors.Add(new AuthenticationInterceptor());
}
}
return sh; })));
这里是
MyServiceHostFactory
的定义:-public MyServiceHostFactory(IocContainer container, Func<ServiceHost, ServiceHost> createservicehost = null);
现在它会抛出以下错误:-
在这一行:-,'System.Collections.Generic.SynchronizedCollection<System.ServiceModel.Dispatcher.IDispatchMessageInspector>.Add(System.ServiceModel.Dispatcher.IDispatchMessageInspector)'的最佳重载方法匹配有一些无效参数。
ed.DispatchRuntime.MessageInspectors.Add(new AuthenticationInterceptor());
我知道原因,是因为我正在尝试在MessageInspector中连接RequestInterceptor。它们都属于不同的接口层次结构。
那么我应该怎么做呢?
编辑:还要注意的是,我无法更改AuthenticationInterceptor逻辑,因为代码不在我的控制范围内。