Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在运行时添加WCF消息检查器_Wcf_Listener_Endpoints_Idispatchmessageinspector - Fatal编程技术网

在运行时添加WCF消息检查器

在运行时添加WCF消息检查器,wcf,listener,endpoints,idispatchmessageinspector,Wcf,Listener,Endpoints,Idispatchmessageinspector,我已经创建了一个自定义ServiceHost,我想使用它自动将消息检查器添加到在其上运行的服务的每个端点。我创建了一个MessageInspector,它实现了IDispatchMessageInspector和IClientMessageInspector,并找到了以下代码,该代码应该将其添加到每个端点: foreach (ChannelDispatcher channel in this.ChannelDispatchers) { foreach (EndpointDispatcher

我已经创建了一个自定义ServiceHost,我想使用它自动将消息检查器添加到在其上运行的服务的每个端点。我创建了一个MessageInspector,它实现了IDispatchMessageInspector和IClientMessageInspector,并找到了以下代码,该代码应该将其添加到每个端点:

foreach (ChannelDispatcher channel in this.ChannelDispatchers) {
  foreach (EndpointDispatcher endpoint in channel.Endpoints) {
      endpoint.DispatchRuntime.MessageInspectors.Add(new MyMessageInspector());
   }
}
This value cannot be changed after a ServiceHost has been opened
我遇到的问题是,在打开servicehost之前,ChannelDispatchers集合是空的,这意味着我无法在构造函数中运行此代码。我为打开的事件创建了一个事件处理程序,并在其中使用了该代码,但在尝试添加端点时出现以下错误:

foreach (ChannelDispatcher channel in this.ChannelDispatchers) {
  foreach (EndpointDispatcher endpoint in channel.Endpoints) {
      endpoint.DispatchRuntime.MessageInspectors.Add(new MyMessageInspector());
   }
}
This value cannot be changed after a ServiceHost has been opened
似乎我陷入了某种陷阱22,我所寻求的功能在WCF中可能实现吗

谢谢


Mike

为了向服务端点添加消息检查器,必须通过实现IServiceBehavior或IEndpointBehavior来完成。对于我最终使用的ServiceBehavior,我将上面的代码放入IServiceBehavior的ApplyDispatch()方法中。然后,我强制地将该行为添加到我的ServiceHost中,尽管我可以通过创建BehaviorExtensionElement进行配置