Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Services / March 2005

Tip: Looking for answers? Try searching our database.

How to log SOAP messages on the client side

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
bill - 26 Mar 2005 02:01 GMT
I know I can use SOAPExtension to read request and response SOAP messages on
the server side for a Web Method. Is there a way to do the same thing on the
client side when you make a Web Service call? I would like to see what is
going out and what is coming in. Thanks for your help.
Breezback - 27 Mar 2005 09:54 GMT
This is the same:

this example was taken from Remoting WebService (Amit Kalani):

using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace BugTrackerClient
{
    /// <summary>
    /// Summary description for SoapDisplayExtension.
    /// </summary>
    public class SoapDisplayExtension : SoapExtension
    {
        public SoapDisplayExtension()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        // Variables to hold the original stream
        // and the stream that we return
        private Stream originalStream;
        private Stream internalStream;

        // Version called if configured with a config file
        public override object GetInitializer(
            System.Type serviceType)
        {
            // Not used in this example, but it//s
            // declared abstract in the base class
            return null;
        }

        // Called the first time the Web service is used
        // Version called if configured with an attribute
        public override object GetInitializer(
            LogicalMethodInfo methodInfo,
            SoapExtensionAttribute attribute)
        {
            // Not used in this example, but it//s
            // declared abstract in the base class
            return null;
        }

        // Called each time the Web service is used
        // And gets passed the data from
        // GetInitializer() method
        public override void Initialize(
            object initializer)
        {
            // Not used in this example, but it//s
            // declared abstract in the base class
        }

        // The ChainStream() method gives us a chance
        // to grab the SOAP messages as they go by
        public override System.IO.Stream ChainStream(
            System.IO.Stream stream)
        {
            // Save the original stream
            originalStream = stream;
            // Create and return our own in its place
            internalStream = new MemoryStream();
            return internalStream;
        }

        // The ProcessMessage() method is where we do our work
        public override void ProcessMessage(
            System.Web.Services.Protocols.SoapMessage message)
        {
            // Determine the stage and take appropriate action
            switch(message.Stage)
            {
                case SoapMessageStage.BeforeSerialize:
                    // About to prepare a SOAP Response
                    break;

                case SoapMessageStage.AfterSerialize:
                    // SOAP response is all prepared
                    internalStream.Position = 0;
                    // Get a Transform ready
                    XslTransform xslt = new XslTransform();
                    xslt.Load(@"..\..\Identity.xslt");
                    // Load the raw XML into an XML document
                    XmlDocument xd = new XmlDocument();
                    xd.Load(internalStream);
                    // Use the transform to pretty print it
                    MemoryStream ms = new MemoryStream();
                    xslt.Transform(xd, null, ms);
                    // And drop the results
                    // to a TextBox control
                    ms.Position = 0;
                    StreamReader sr = new StreamReader(ms);
                    DisplayMessage f = new DisplayMessage();
                    f.txtMessage.Text = sr.ReadToEnd();
                    f.Show();
                    // Copy the passed message
                    // to the other stream
                    internalStream.Position = 0;
                    CopyStream(
                        internalStream, originalStream);
                    internalStream.Position = 0;
                    break;

                case SoapMessageStage.BeforeDeserialize:
                    // About to handle a SOAP request
                    // Copy the passed message
                    // to the other stream
                    CopyStream(
                        originalStream, internalStream);
                    internalStream.Position = 0;
                    // Get a Transform ready
                    XslTransform xslt1 = new XslTransform();
                    xslt1.Load(@"..\..\Identity.xslt");
                    // Load the raw XML into an XML document
                    XmlDocument xd1 = new XmlDocument();
                    xd1.Load(internalStream);
                    // Use the transform to pretty print it
                    MemoryStream ms1 = new MemoryStream();
                    xslt1.Transform(xd1, null, ms1);
                    // And drop the results
                    // to a TextBox control
                    ms1.Position = 0;
                    StreamReader sr1 = new StreamReader(ms1);
                    DisplayMessage f1 = new DisplayMessage();
                    f1.txtMessage.Text = sr1.ReadToEnd();
                    f1.Show();
                    internalStream.Position = 0;
                    break;

            case SoapMessageStage.AfterDeserialize:
                    // SOAP request has been deserialized
                    break;
            }
        }

        // Helper function to copy one stream to another
        private void CopyStream(Stream fromStream,
            Stream toStream)
        {
            try
            {
                StreamReader sr  =
                    new StreamReader(fromStream);
                StreamWriter sw =
                    new StreamWriter(toStream);
                sw.WriteLine(sr.ReadToEnd());
                sw.Flush();
            }
            catch (Exception ex)
            {
                // Log the exception
            }
        }
    }
}

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.