简体   繁体   English

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

[英]how to put inspector on both client and server 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. 我听说可以将检查器放在客户端和服务器wcf端,这样可以在将消息发布到客户端/服务器之前拦截消息并修改其内容。 i want to know is it possible if yes then how? 我想知道是否可以,怎么办?

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. 我在Google上搜索有关该主题的文章,但不幸的是我没有。 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. 因此,如果有人知道讨论如何开发这种检查器并将其部署在wcf客户端和服务器上的任何URL,请与我分享。 thanks 谢谢

All what you need is to follow this steps 您所需要做的就是遵循以下步骤

1-step 1步

In your code you should add a MessageInspectorExtension 在您的代码中,您应该添加一个MessageInspectorExtension

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

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

MessageInspector will do all the work you need and it should be defined like this MessageInspector将完成您需要的所有工作,并且应该这样定义

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

The mains methods you need are 您需要的主要方法是

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 

and

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-step 2步

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-step “三步走

<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 Normally a https connection will do the job for you see this link how could i encrypt/decrypt data with our own logic at both end when data just passed通常情况下,https连接会完成此工作,您会看到此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 但是,如果您需要一种棘手的方法来实现此目的,则可以使用一个通用类,该通用类将是您进行所有通信的主类,并在其中一个属性中对消息进行加密

Something like this 像这样

 [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 

        }
    }

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

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