简体   繁体   English

在回调方向检查WCF消息?

[英]Inspect WCF Messages in the Callback direction?

I can inspect WCF messsages on both Client side and server side using IClientMessageInspector, IDispatchMessageInspector respectively. 我可以分别使用IClientMessageInspector,IDispatchMessageInspector检查客户端和服务器端的WCF消息。 But in a Duplex comunications it is not clear how to do it in a callback from server to client (Nor much documentation on that topic). 但是在双工通信中,如何在从服务器到客户端的回调中做到这一点尚不清楚(关于该主题的文档也不多)。

Any ideas about how to implement this feature? 有关如何实现此功能的任何想法?

Finally I get the solution. 最后我得到了解决方案。

In a Duplex comunication scenario when a callback is made the server becomes the client and vice versa. 在双工通信方案中,当进行回调时,服务器成为客户端,反之亦然。

So on server side when implementing IServiceBehavior inject the message inspector using the CallbackClientRuntime property of the DispatchRuntime foreach EndpointDispatcher. 因此,在服务器端实现IServiceBehavior时,使用DispatchRuntime foreach EndpointDispatcher的CallbackClientRuntime属性注入消息检查器。

public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
    foreach (ChannelDispatcher item in serviceHostBase.ChannelDispatchers)
    {
        foreach (EndpointDispatcher epd in item.Endpoints)
        {
            //injecting an inspector in normal call
            epd.DispatchRuntime.MessageInspectors.Add(new MessageSizerInspector());

            //injecting an inspector in callback
            epd.DispatchRuntime.CallbackClientRuntime.MessageInspectors.Add(new MessageSizerInspector());
        }
    }
}

On client side when implementing IEndpointBehavior inject the message inspector using the CallbackDispatchRuntime. 在客户端实现IEndpointBehavior时,使用CallbackDispatchRuntime注入消息检查器。

public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
    //injecting an inspector in normal call
    clientRuntime.MessageInspectors.Add(new MessageSizerInspector());

    //injecting an inspector in callback
    clientRuntime.CallbackDispatchRuntime.MessageInspectors.Add(new MessageSizerInspector());       
}

Then apply the extension as always. 然后一如既往地应用扩展名。

In my case I created a class like the following pseudo code 在我的例子中,我创建了一个类,如下面的伪代码

public class MessageSizer : Attribute, IServiceBehavior, IEndpointBehavior
{
    .....
}

then I applied this attribute to service implementation for the server side inspection and added a behaviorExtensions inside the app.config to setup the endpoint for message inspection on client side. 然后我将此属性应用于服务器端检查的服务实现,并在app.config中添加了behaviorExtensions,以便在客户端设置端点以进行消息检查。

<system.serviceModel>
    ...........
    <client>
      <endpoint address="net.tcp://localhost/MinerDual.svc"
            binding="netTcpBinding" bindingConfiguration="wsDualMinerNetTcp"
            contract="WebApplication.IMinerDual" name="NetTcpMinerDual" 
            behaviorConfiguration="Default" />
    </client>
  <behaviors>
    <endpointBehaviors >
      <behavior name="Default">
        <messageSizer/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <extensions>
    <behaviorExtensions>
      <add name="messageSizer"
           type="WCFExtensions.MessageSizerElement, WCFExtensions, 
           Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    </behaviorExtensions>
  </extensions>
</system.serviceModel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM