locked
Printing whole SOAP envelope - how? RRS feed

  • Question

  • Hi all,

    Is there a way to print a full SOAP request ? It's very nice that in WCF I can only define service interface and do the implementation straight away, without caring much for processing soap envelopes etc. But for debug reasons, sometimes I'd like to get the content of the raw SOAP request that I'm processing and store it somewhere in a file. Is there an easy way to get the soap message recieved by the service (from within the operation implementation code)?
    Wednesday, January 13, 2010 5:11 PM

Answers

  • Hi Pawel,

    As Carlos suggested, you can create a custom messageInspector to intercept the underlying soap message of WCF service operation's request/response. And here are two blog articles that also provide some examples on this:

    #Writing a WCF Message Inspector
    http://weblogs.asp.net/paolopia/archive/2007/08/23/writing-a-wcf-message-inspector.aspx

    #[WCF]How to inspect and modify WCF message via custom MessageInspector
    http://blogs.msdn.com/stcheng/archive/2009/02/21/wcf-how-to-inspect-and-modify-wcf-message-via-custom-messageinspector.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, January 15, 2010 6:24 AM
  • You can use an IDispatchMessageInspector to look at the incoming messages (and log them). The example below shows an example of that.

        class Post_31a6dc8b_91a2_4aab_94b1_bfd93565236d
        {
            [DataContract]
            public class Person
            {
                [DataMember]
                public string Name { get; set; }
                [DataMember]
                public int Age { get; set; }
            }
            [ServiceContract]
            public interface ITest
            {
                [OperationContract]
                Person EchoPerson(Person input);
            }
            public class Service : ITest
            {
                public Person EchoPerson(Person input)
                {
                    return input;
                }
            }
            public class MyInspector : IDispatchMessageInspector, IEndpointBehavior
            {
                public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
                {
                }
    
                public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
                {
                }
    
                public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
                {
                    endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
                }
    
                public void Validate(ServiceEndpoint endpoint)
                {
                }
    
                public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
                {
                    Console.WriteLine("Request: {0}", request);
                    return null;
                }
    
                public void BeforeSendReply(ref Message reply, object correlationState)
                {
                }
            }
            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(), "");
                endpoint.Behaviors.Add(new MyInspector());
                host.Open();
    
                ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
                ITest proxy = factory.CreateChannel();
                proxy.EchoPerson(new Person { Name = "John Doe", Age = 25 });
    
                ((IClientChannel)proxy).Close();
                factory.Close();
                host.Close();
            }
        }
    
    Wednesday, January 13, 2010 5:46 PM

All replies

  • You can use an IDispatchMessageInspector to look at the incoming messages (and log them). The example below shows an example of that.

        class Post_31a6dc8b_91a2_4aab_94b1_bfd93565236d
        {
            [DataContract]
            public class Person
            {
                [DataMember]
                public string Name { get; set; }
                [DataMember]
                public int Age { get; set; }
            }
            [ServiceContract]
            public interface ITest
            {
                [OperationContract]
                Person EchoPerson(Person input);
            }
            public class Service : ITest
            {
                public Person EchoPerson(Person input)
                {
                    return input;
                }
            }
            public class MyInspector : IDispatchMessageInspector, IEndpointBehavior
            {
                public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
                {
                }
    
                public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
                {
                }
    
                public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
                {
                    endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
                }
    
                public void Validate(ServiceEndpoint endpoint)
                {
                }
    
                public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
                {
                    Console.WriteLine("Request: {0}", request);
                    return null;
                }
    
                public void BeforeSendReply(ref Message reply, object correlationState)
                {
                }
            }
            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(), "");
                endpoint.Behaviors.Add(new MyInspector());
                host.Open();
    
                ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
                ITest proxy = factory.CreateChannel();
                proxy.EchoPerson(new Person { Name = "John Doe", Age = 25 });
    
                ((IClientChannel)proxy).Close();
                factory.Close();
                host.Close();
            }
        }
    
    Wednesday, January 13, 2010 5:46 PM
  • Hi Pawel,

    As Carlos suggested, you can create a custom messageInspector to intercept the underlying soap message of WCF service operation's request/response. And here are two blog articles that also provide some examples on this:

    #Writing a WCF Message Inspector
    http://weblogs.asp.net/paolopia/archive/2007/08/23/writing-a-wcf-message-inspector.aspx

    #[WCF]How to inspect and modify WCF message via custom MessageInspector
    http://blogs.msdn.com/stcheng/archive/2009/02/21/wcf-how-to-inspect-and-modify-wcf-message-via-custom-messageinspector.aspx


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, January 15, 2010 6:24 AM