0

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?

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

Thomas
  • 33,544
  • 126
  • 357
  • 626

1 Answers1

0

All what you need is to follow this steps

1-step

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

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

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

        }
    }
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
  • there is no relationship between MessageInspectorExtension & MessageInspector. – Thomas Apr 02 '14 at 10:42
  • is there any article available from where i can get the full source code. which keyword i should use to search google for the above scenario. – Thomas Apr 02 '14 at 10:50
  • you can find a full example in http://go.microsoft.com/fwlink/?LinkId=87352 after you download and install the samples try to find \WCF\Extensibility\MessageInspectors\SchemaValidation\CS – BRAHIM Kamel Apr 02 '14 at 11:05