WCF: Implementing Message Inspector at Client Side.

In this article I will show you how to implement Message Inspector at client side.

Message Inspector

Message Inspector provides us having inspection code at Client Side for Inspecting code before Request is sent to Service.

and Provides us having inspection code at Service side for inspecting code before response is sent to Client.

1) To implement this first we need to implement MessageInspector by inheriting Interface “IClientMessageInspector”. Here you can implement 2 methods

  • AfterReceiveReply
  • BeforeSendRequest
class MessageInpector : IClientMessageInspector


       public void AfterReceiveReply(ref System.ServiceModel.Channels.Message

        reply,object correlationState)




       public object BeforeSendRequest(ref System.ServiceModel.Channels.Message

        request, System.ServiceModel.IClientChannel channel)


           //Handle Request Validation here

           return null;



2) Now we need to define BEHAVIOR, by inheriting IEndpointBehavior. Here we need to override method call ApplyClientBehavior. In this method you need to add the your MessageInspector object to behavior. See code snippet below.

class CustomBehavior : IEndpointBehavior


        public void AddBindingParameters(ServiceEndpoint serviceEndpoint,

         System.ServiceModel.Channels.BindingParameterCollection bindingParameters)

        { }


        public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint,

        System.ServiceModel.Dispatcher.ClientRuntime behavior)


            //Add the inspector

            behavior.MessageInspectors.Add(new MessageInpector());



        public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint,

        System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)



       public void Validate(ServiceEndpoint serviceEndpoint)




3) Now you can add behavior to your proxy object as show in below code.

ServiceReference1.Service1Client myclient = new ServiceReference1.Service1Client();

           //System.ServiceModel.Channels.Message mymsg = System.ServiceModel.


           //Channels.MessageVersion.Default, "gg", "Hello");

           myclient.Endpoint.Behaviors.Add(new CustomBehavior());



and the Config file is shown below




                <binding name="WcfServicewithMessageInpector.Service1" closeTimeout="00:01:00"

                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"

                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"

                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"

                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"


                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"

                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />

                    <security mode="None">

                        <transport clientCredentialType="None" proxyCredentialType="None"

                            realm="" />

                        <message clientCredentialType="UserName" algorithmSuite="Default" />






            <endpoint address="http://localhost:55737/Service1.svc" binding="basicHttpBinding"


                contract="ServiceReference1.IService1" name="WcfServicewithMessageInpector.Service1" />




That’s all, So by using Message Inspector you can inspect the actual message which is sent to Service or inspect the message which is received from Service at client side.

Same way we can Implement Message Inspector at Service Side to inspects the massage.


One thought on “WCF: Implementing Message Inspector at Client Side.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s