none
Programitically View Soap Messages in WCF ?

    Question


  • Hello,

    I need to write an application that displays the wcf generated soap messages ?

    any idea on how to achieve this ?

    Regards

    yama
    Monday, December 17, 2007 7:23 AM

Answers

  • The easiest way is to use a message inspector (either an IClientMessageInspector to display the messages at the client side, or an IDispatchMessageInspector to display the messages at the server side). The code below shows an example of that.

     

    public class Post2562869

    {

        [ServiceContract]

        public interface ITest

        {

            [OperationContract]

            string Echo(string text);

            [OperationContract]

            int Add(int x, int y);

        }

        public class Service : ITest

        {

            public string Echo(string text)

            {

                return text;

            }

            public int Add(int x, int y)

            {

                return x + y;

            }

        }

        public class MyInspector : IClientMessageInspector, IEndpointBehavior

        {

            public List<string> sentMessages = new List<string>();

            public List<string> receivedMessages = new List<string>();

     

            #region IClientMessageInspector Members

            public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)

            {

                receivedMessages.Add(reply.ToString());

            }

            public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)

            {

                sentMessages.Add(request.ToString());

                return null;

            }

            #endregion

     

            #region IEndpointBehavior Members

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

            {

            }

            public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)

            {

                clientRuntime.MessageInspectors.Add(this);

            }

            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)

            {

            }

            public void Validate(ServiceEndpoint endpoint)

            {

            }

            #endregion

        }

        public static void Test()

        {

            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";

            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));

            ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");

            host.Open();

            Console.WriteLine("Host opened at " + baseAddress);

     

            MyInspector inspector = new MyInspector();

            ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));

            factory.Endpoint.Behaviors.Add(inspector);

            ITest proxy = factory.CreateChannel();

     

            proxy.Echo("Hello world");

            proxy.Add(123, 456);

     

            ((IClientChannel)proxy).Close();

            factory.Close();

            host.Close();

     

            Console.WriteLine("Sent messages:");

            foreach (string sent in inspector.sentMessages)

            {

                Console.WriteLine(sent);

                Console.WriteLine();

            }

            Console.WriteLine("*******************************************");

     

            Console.WriteLine("Received messages:");

            foreach (string received in inspector.receivedMessages)

            {

                Console.WriteLine(received);

                Console.WriteLine();

            }

        }

    }

     

    For more information, see the msdn doc for those interfaces.

    Monday, December 17, 2007 2:36 PM

All replies

  • The easiest way is to use a message inspector (either an IClientMessageInspector to display the messages at the client side, or an IDispatchMessageInspector to display the messages at the server side). The code below shows an example of that.

     

    public class Post2562869

    {

        [ServiceContract]

        public interface ITest

        {

            [OperationContract]

            string Echo(string text);

            [OperationContract]

            int Add(int x, int y);

        }

        public class Service : ITest

        {

            public string Echo(string text)

            {

                return text;

            }

            public int Add(int x, int y)

            {

                return x + y;

            }

        }

        public class MyInspector : IClientMessageInspector, IEndpointBehavior

        {

            public List<string> sentMessages = new List<string>();

            public List<string> receivedMessages = new List<string>();

     

            #region IClientMessageInspector Members

            public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)

            {

                receivedMessages.Add(reply.ToString());

            }

            public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel)

            {

                sentMessages.Add(request.ToString());

                return null;

            }

            #endregion

     

            #region IEndpointBehavior Members

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

            {

            }

            public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)

            {

                clientRuntime.MessageInspectors.Add(this);

            }

            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)

            {

            }

            public void Validate(ServiceEndpoint endpoint)

            {

            }

            #endregion

        }

        public static void Test()

        {

            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";

            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));

            ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");

            host.Open();

            Console.WriteLine("Host opened at " + baseAddress);

     

            MyInspector inspector = new MyInspector();

            ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));

            factory.Endpoint.Behaviors.Add(inspector);

            ITest proxy = factory.CreateChannel();

     

            proxy.Echo("Hello world");

            proxy.Add(123, 456);

     

            ((IClientChannel)proxy).Close();

            factory.Close();

            host.Close();

     

            Console.WriteLine("Sent messages:");

            foreach (string sent in inspector.sentMessages)

            {

                Console.WriteLine(sent);

                Console.WriteLine();

            }

            Console.WriteLine("*******************************************");

     

            Console.WriteLine("Received messages:");

            foreach (string received in inspector.receivedMessages)

            {

                Console.WriteLine(received);

                Console.WriteLine();

            }

        }

    }

     

    For more information, see the msdn doc for those interfaces.

    Monday, December 17, 2007 2:36 PM

  • Hey solved my problem...

    thanks a ton !
    Tuesday, December 18, 2007 5:22 AM