如何将检查器放在客户端和服务器 WCF 上

  • 本文关键字:客户端 服务器 WCF c# wcf
  • 更新时间 :
  • 英文 :


我听说可以将检查器放在客户端和服务器WCF端,它可以拦截消息并在消息发布到客户端/服务器之前修改其内容。 我想知道如果是,那怎么可能?

当数据刚刚通过时,我如何在两端使用我们自己的逻辑加密/解密数据。 我搜索谷歌有一些关于这个主题的好文章,但不幸的是我没有得到。 因此,如果有人知道任何讨论如何开发这种检查器并在WCF客户端和服务器上部署的URL,那么请与我分享。

谢谢

您所需要的只是按照以下步骤操作

1 步

在你的代码中,你应该添加一个 MessageInspectorExtension

 class MessageInspectorExtension : BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get { return typeof(MessageInspector); }
        }
        protected override object CreateBehavior()
        {
            return new MessageInspector();
        }
    }

MessageInspector将完成您需要的所有工作,并且应该像这样定义

//here the `MessageInspector` class showing what are the interfaces responsable for doing this  
public class MessageInspector : IDispatchMessageInspector, IServiceBehavior, IEndpointBehavior
    {
    }

您需要的电源方法是

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {}
Which is called after an inbound message has been received but before the message is dispatched to the intended operation 

public void BeforeSendReply(ref Message reply, object correlationState)
        {
            if (reply.IsFault)
            {//do here }
            else do your code here  
       }
wich is called after the operation  has returned but before the reply message is sent 

2 步

在应用配置中,必须添加如下所示的行为定义

    <behaviors>
          <endpointBehaviors>           
            <behavior name="ServiceBehaviorWithInterceptor">
              <ServiceInspector/>
            </behavior>
          </endpointBehaviors>
        <behaviors>
<!--Extensions-->   
     <extensions>
          <behaviorExtensions>
            <add name="ServiceInspector" type="yourNameSpace.MessageInspector.MessageInspectorExtension, assemblyname, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
          </behaviorExtensions>
        </extensions>

"三步走

<service name="yourServiceContractFullName" behaviorConfiguration="ServiceBehaviorWithInterceptor">
        <host>
          <baseAddresses>
            <add baseAddress="http://yourserver:port/root/yourServiceContract" />
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpsBindingConfig" contract="YourContract" />
      </service>

Update

how could i encrypt/decrypt data with our own logic at both end when data just passed 通常,https连接将为您完成这项工作,请参阅此link
但是,如果您需要一种棘手的方法来实现这一目标,则可以使用一个公共类,该类将成为所有通信的主类,并在其中一个属性中加密消息。

像这样的东西

 [DataContract]
     public class BaseCustomEncMessage
    {
        private Object _object = null;  
        [DataMemberAttribute]
        public Object CipheredMessage { get { return _object; } }
        public void Cipher(object obj )
        {
             //here you can use 3DES for example and serialize your instance as an object 
        }
    }

相关内容

最新更新