我在这里有两个疑问:-
1)两者的基本区别是什么Microsoft.ServiceModel.Web.RequestInterceptor and System.ServiceModel.Dispatcher.DispatchRuntime.MessageInspectors (IdispatchMessageInterceptor)
两者似乎都是请求/消息拦截器,可用于在请求管道中实现自定义验证/拦截器。
什么时候用一个对另一个?
RequestInterceptor
2 )还有如何插入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);
现在它抛出以下错误: -
The best overloaded method match for 'System.Collections.Generic.SynchronizedCollection<System.ServiceModel.Dispatcher.IDispatchMessageInspector>.Add(System.ServiceModel.Dispatcher.IDispatchMessageInspector)' has some invalid arguments
在这一行: -
ed.DispatchRuntime.MessageInspectors.Add(new AuthenticationInterceptor());
我知道为什么,只是因为我试图在 MessageInspector 中连接 RequestInterceptor。两者都有不同的接口层次结构。
那么我应该在这里做什么?
编辑:
另请注意,我无法将 AuthenticationInterceptor 逻辑更改为不受我控制的代码。