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

问题描述:

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

i heard that it is possible to to put inspector on both client and server wcf side which can intercept the message and modify it's content before it released to the client/server. i want to know is it possible if yes then how?

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

how could i encrypt/decrypt data with our own logic at both end when data just passed. i search Google to have some good write up on this topic but unfortunately i got none. so if anyone knows about any url which discuss how to develop this kind of inspector and deploy at both wcf client & server then please share with me. thanks

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

All what you need is to follow this steps

第一步

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

In your code you should add a MessageInspectorExtension

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

        protected override object CreateBehavior()
        {
            return new MessageInspector();
        }
    }

MessageInspector 会做你需要的所有工作,它应该像这样定义

MessageInspector will do all the work you need and it should be defined like this

//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 

两步

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

In your app config you have to add a behavior definition like the following

    <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>

3步

<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>

更新

当数据刚刚通过时,我如何在两端用我们自己的逻辑加密/解密数据通常,https 连接将为您完成这项工作,请参见 link
但是如果您需要一种棘手的方法来实现这一点,您可以使用一个通用类,该类将成为您所有通信的主要类,并在其属性之一中加密您的消息

how could i encrypt/decrypt data with our own logic at both end when data just passed Normally a https connection will do the job for you see this link
but if you need a tricky way to achieve this you can use a common class that will be the main class for all you communications and encrypt your message in one of it's property

类似的东西

 [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 

        }
    }