WCF - Inspect the messages being sent/received?

后端 未结 6 1741
难免孤独
难免孤独 2020-12-13 03:51

I have 2 solutions: - Server Solution - Client Solution

The server registers itself to my localhost IIS: http://localhost/MyApp/

The client adds WCF Servic

相关标签:
6条回答
  • 2020-12-13 04:28

    Maybe I'm missing something, but... Why don't you use the WCF tracing features? It's a fantastic troubleshooting tool. I've used it for services hosted in IIS/WAS also.

    Enabling WCF Tracing

    BTW, some people don't know it, but you can open traces from server side and client side at the same time, and the Viewer will show you the correlation between the server and client actions in a nice graph.

    EDIT: whenever I had to capture TCP/IP traffic, I use WireShark. If you need to do it programatically you can use SharpPCAP, so I can take actions upon what I capture from the network. But for troubleshooting, much much better to rely on WCF Tracing.

    0 讨论(0)
  • 2020-12-13 04:30

    Look at this StackOverflow thread: How to use Fiddler to monitor WCF service

    It answers some of your questions. you can also use something like WireShark if you want to examine everything on the wire rather than set up a Proxy, as Fiddler does.

    0 讨论(0)
  • 2020-12-13 04:46

    Is your service SOAP or RESTful? You can use the WCF Service Trace Viewer Tool to view the SOAP message headers and bodies. Instructions for configuring your web service for tracing are here.

    0 讨论(0)
  • 2020-12-13 04:46

    In WCF we can use another way to see actual SOAP messages - custom MessageEncoder - a low-level pipeline extensibility point. Unlike message inspectors (IDispatchMessageInspector / IClientMessageInspector) it sees original byte content including any malformed XML data. You need to wrap a standard textMessageEncoding as custom binding element and adjust config file to use that custom binding.

    Also you can see as example how I did it in my project - wrapping textMessageEncoding, logging encoder, custom binding element and config.

    0 讨论(0)
  • 2020-12-13 04:50

    To view the message contents you must add a source for System.ServiceModel.MessageLogging in your configuration file. The message tab in the Trace Viewer will show the full message for a particular service call.

    Here is a sample configuration file:

    <configuration>
    
    ...
    
       <system.diagnostics>
          <sources>
             <source name="System.ServiceModel"
                          switchValue="All"
                          propagateActivity="true">
                <listeners>
                   <add name="traceListener" />
                </listeners>
             </source>
             <source name="System.ServiceModel.MessageLogging"
                          switchValue="All">
                <listeners>
                   <add name="traceListener" />
                </listeners>
             </source>
          </sources>
          <sharedListeners>
             <add name="traceListener"
                     type="System.Diagnostics.XmlWriterTraceListener"
                     initializeData="c:\Traces.svclog" />
          </sharedListeners>
       </system.diagnostics>
    
       <system.serviceModel>
       <diagnostics>
          <messageLogging logEntireMessage="true"
                                      logMalformedMessages="true"
                                      logMessagesAtServiceLevel="true"
                                      logMessagesAtTransportLevel="true"
                                      maxMessagesToLog="500"/>
       </diagnostics>
    
    ...
    
    </system.serviceModel>
    
    ...
    
    </configuration>
    

    See the Configuring Tracing topic on MSDN for more information. http://msdn.microsoft.com/en-us/library/ms733025.aspx

    0 讨论(0)
  • 2020-12-13 04:51

    If you want to inspect messages programattically, you can implement an IClientMessageInspector interface and register it with your client.

    This allows you access to all of the messages, no matter what binding you are using, whereas using tools like Fiddler will only allow you to inspect messages using the HTTP transport channel.

    Note, that using this technique, you can do much actually modify the messages or do much more (fire off notifications, for example). If all you wish to do is put your eyes on the message, then using tracing might be an easier approach for you.

    0 讨论(0)
提交回复
热议问题