Tuesday, May 24, 2011

WCF Service Diagnostics Part 1

Sometimes its useful to be able to examine the raw soap messages coming into and out of a service.  Especially when trying to interface into a third party service or client. Quite often all you get is WSDL and not much else.

Back in the days of ASMX Web Services you had to write your own SoapExtension to be able get access to the SOAP/XML. Including all the tedious IO mechanics of writing the log information to an external source. WCF has a built in logging mechanism that just needs to be turned on.  In addition WCF also has its own log viewer.

Enabling WCF Logging
This requires a few modifications to your web.config.  Alternatively you can use the WCF Service Configuration Editor tool from Visual Studio's Tools menu (or located on your disk at: <Program Files (x86)>\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools)


<?xml version="1.0"?>
<configuration>
    <system.diagnostics>
        <sources>
            <source 
                name="System.ServiceModel.MessageLogging" 
                switchValue="Off,ActivityTracing">
                <listeners>
                    <add 
                        type="System.Diagnostics.DefaultTraceListener" 
                        name="Default">
                        <filter type="" />
                    </add>
                    <add name="ServiceModelMessageLoggingListener">
                        <filter type="" />
                    </add>
                </listeners>
            </source>
        </sources>
        <sharedListeners>
            <add 
                initializeData="C:\web_messages.svclog" 
                type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                name="ServiceModelMessageLoggingListener" 
                traceOutputOptions="Timestamp">
                <filter type="" />
            </add>
        </sharedListeners>
        <trace autoflush="true" />
    </system.diagnostics>
    
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    
    <system.serviceModel>
        <diagnostics>
            <messageLogging 
                logEntireMessage="true" 
                logMalformedMessages="false"
                logMessagesAtServiceLevel="true" 
                logMessagesAtTransportLevel="false" />
        </diagnostics>

...




Microsoft Service Trace Viewer
To view a log file double-clicking the file will open it if you leave the extension as *.svclog, The viewer is installed as part of Visual Studio 2010 and will be located on your disk <Program Files (x86)>\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools).



The complete soap message envelope is shown inside the application data element.  On the left pane use the messages tab to show only messages and look for the items that have a named action. This is the action name used in the OperationContract attributes. Or if left blank it will be as shown above using the TempUri namespace.

Recommended Settings for Production and Development Debugging etc are recommended by Microsoft here:
http://msdn.microsoft.com/en-us/library/aa702726.aspx


WCF Test Client
Located at <Program Files (x86)>\Microsoft Visual Studio 10.0\Common7\IDE) and is installed as part of Visual Studio. This is a great simple utility for quickly testing services of any kind.  In fact it will auto start when using the Visual Studio WCF Application project template.

Its great for quick testing of services and viewing request and response xml.






Packet Sniffing
http://www.pocketsoap.com/tcptrace/
This is useful for sniffing packets at a lower level particularly when you are writing a client that is interfacing into a third party non-Http service.

For Http services it will probably be easier using Fiddler.




References:

No comments:

Post a Comment