Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Billy McCafferty



Examine XML of Web Service Response

While working on winning Netflix' million, I came across a need to examine the raw XML returned by a third party web service.  After a couple desk-pounding hours, I finally found a simple solution which works equally well in both Web and Windows apps.

  1. Install Web Services Enhancements (WSE) 3.0
  2. Add a project reference to Microsoft.Web.Services3
  3. Modify the web service proxy to inherit from Microsoft.Web.Services3.WebServicesClientProtocol instead of System.Web.Services.Protocols.SoapHttpClientProtocol.  (This augments the proxy's capabilities.)
  4. The response's raw XML may then be retrieved immediately after the call to Invoke as ResponseSoapContext.Envelope.InnerXml

Optionally, log4net may be employed so as not to interrupt the application while it goes about its business...

  1. Add a project reference to log4net
  2. Add log4net configuration to app.config or web.config.  For example:

    <!-- If you want to turn log4net off completely,
    include threshold="OFF" below
    -->

    <log4net threshold="ON">

        <appender name="LogAllToFile"
            type="log4net.Appender.FileAppender">

            <file value="MyLog.log"/>

            <appendToFile value="false"/>

            <layout type="log4net.Layout.PatternLayout">

                <conversionPattern
                    value="%d [%t] %-5l - %m%n%n"/>

            </layout>

        </appender>

        <root>

            <!-- ALL, DEBUG, INFO, WARN,
            ERROR, FATAL, OFF
    -->

            <priority value="ALL"/>

            <appender-ref ref="LogAllToFile"/>

        </root>

    </log4net>

  3. Initialize log4net when the application starts with a call to XmlConfigurator.Configure();
  4. Add the following code immediately after the call to Invoke in the proxy web service:

    log4net.ILog logger = log4net.LogManager.GetLogger(typeof(MyProxyClassName));

    logger.Info(ResponseSoapContext.Envelope.InnerXml);

With log4net turned on, the raw XML within the web service response will get written out to the log file and the application will not be interrupted.

Billy



Check out Devlicio.us!

Our Sponsors

Proudly Partnered With