How to read incoming and outgoing message in wcf

 

In this post,I will show you how to read outgoing and incoming message in wcf.

Create a new console application and add following assembly reference in the project

using System.ServiceModel;
using System.ServiceModel.Description;

and paste following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WCF_MessageInspector
{
    [ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        string Greet(string name);
    }
    [ConsoleServiceBehavior]
    [ServiceBehavior]
    public class HelloService : IHelloService
    {
        public string Greet(string name)
        {
            return "Hello ," + name;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string baseUrl = "http://localhost:12345";
            ServiceHost host = new ServiceHost(typeof(HelloService), new Uri(baseUrl));
            host.AddServiceEndpoint(typeof(IHelloService), new BasicHttpBinding(), "basic");
            // Check to see if the service host already has a ServiceMetadataBehavior
            ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
            // If not, add one
            if (smb == null)
                smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            host.Description.Behaviors.Add(smb);

            host.Open();
            Console.WriteLine("Service is ready");
            Console.Read();
        }
    }
}
Create a new class named  ConsoleMessageInspector and inherit it from following interfaces
IClientMessageInspector
IDispatchMessageInspector
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;

namespace WCF_MessageInspector
{
    public class ConsoleMessageInspector : IClientMessageInspector, IDispatchMessageInspector
    {
        public Message CreateMessage(Message message)
        {
            MessageBuffer buffer = message.CreateBufferedCopy(Int32.MaxValue);
            var messageCopy = buffer.CreateMessage();
            Console.WriteLine(messageCopy.ToString());
            return buffer.CreateMessage();

        }
        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
            request = CreateMessage(request);
            return null;
        }

        public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            reply = CreateMessage(reply);
        }

        public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            reply = CreateMessage(reply);
        }

        public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
        {
            request = CreateMessage(request);
            return null;
        }
    }
}
Create a new class named ConsoleEndpointBehavior and inherit it from following iterface
IEndpointBehavior
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;

namespace WCF_MessageInspector
{
   public class ConsoleEndpointBehavior:IEndpointBehavior
    {

        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(new ConsoleMessageInspector());
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new ConsoleMessageInspector());
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            
        }
    }
}
Now it’s time to apply custom inspector to the service.For this create a new class named  ConsoleServiceBehavior and inherit it from Attribute and IServiceBehavior
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Description;

namespace WCF_MessageInspector
{
   public class ConsoleServiceBehavior:Attribute,IServiceBehavior
    {
        public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            foreach (var item in serviceHostBase.Description.Endpoints)
            {
                item.Behaviors.Add(new ConsoleEndpointBehavior());
            }
        }

        public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            
        }
    }
}

Create a new Project named client and add service reference

Untitled

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            Proxy.HelloServiceClient client = new Proxy.HelloServiceClient();
            Console.WriteLine(client.Greet("Bill"));
        }
    }
}

 

Run both the application,and you will show that incoming and outgoing messages are captured

 

output

Complete code

6 comments:

  1. Dear Blogger,

    This is Lacy rom e-iceblue, I am coming here in order to invite you to write a short article by using Spire.DataExport which is .net data export component that can export data from database, listview, command to word, excel,pdf,rtf,html,xml,ms access, sylk,dif,rtf,text etc. I believe many of your readers may like it. You can see the whole project here: https://exportdata.codeplex.com/
    and if you do not mind, you can see the demos here: http://www.e-iceblue.com/Knowledgebase/Spire.DataExport/Demos.html then choose one that you are interested in. Thanks, please tell me if you accept my invitaion.

    ReplyDelete
  2. nice sharing this code is very useful to me.... thank you

    ReplyDelete
  3. {

    http://tempuri.org/IHelloService/GreetResponse


    Hello ,wasif

    }
    I want this response is it possible to make any change in create message function..

    ReplyDelete
  4. This code is very useful to me,thank you for sharing this code.For more details please visit our website..
    Oracle Fusion Training Institute

    ReplyDelete
  5. I would like to appreciate your work for good accuracy and got informative knowledge from here.
    Oracle Fusion HCM Technical Training

    ReplyDelete