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