2012-01-17 177 views
1

作为我进入WCF的风险的一部分,我正在查看消息契约并查看它们如何影响SOAP消息的内容。如何拦截SOAP消息

如果你能截取这条信息并看看它的结构如何,那真的很酷。 (到目前为止,我已经看过Wireshark(太低级)了,想到了微软的SOAP工具包,但是2005年微软公司退休了)

回答

4

当您安装.NET 3.5或更高版本时,您的机器上应该有WCF Test Client(隐藏在C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\之类的目录内的深处)。

这个工具允许你连接到你的WCF服务,你可以调用它的方法 - 你可以看看它的所有美丽的XML请求和响应:-)

enter image description here

的另一种选择是使用类似的SoapUI免费的版本,旨在测试SOAP服务并显示请求和响应的XML

enter image description here

SoapUI是一个很好的工具 - 但它不是而是 WCF特有的,它只是一个“通用”的SOAP/WSDL工具,可以很好地对付任何SOAP服务。

如果您不是在寻找“按需”捕获请求和响应,但如果您对追踪所有请求和响应更感兴趣,则应调查WCF tracing features并根据需要进行设置。您可以将所有流量捕获到磁盘上的*.svclog文件中,并且还有WCF Service Trace Viewer(也与WCF一起免费)检查这些跟踪文件。

+0

谢谢; WCF测试客户端正是我所期待的 – SkeetJon

+1

Freakin真棒!这是一个非常有用的小工具!谢谢 –

1

如果你想要写日志记录是试图在这个环节上使用TraceExtension类,下面一个例子, 您还会发现,我用它如何实现它的细节和它的工作非常出色

http://www.systemdeveloper.info/2013/11/trace-soap-requestresponse-xml-with.html

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Web.Services.Protocols; 
using System.IO; 
using System.Xml; 

namespace PruebaServiciosNBC 
{ 
    class TraceExtension : SoapExtension 
    { 
     private Stream oldStream; 
     private Stream newStream; 

     private static XmlDocument xmlRequest; 
     /// <summary> 
     /// Gets the outgoing XML request sent to PayPal 
     /// </summary> 
     public static XmlDocument XmlRequest 
     { 
      get { return xmlRequest; } 
     } 

     private static XmlDocument xmlResponse; 
     /// <summary> 
     /// Gets the incoming XML response sent from PayPal 
     /// </summary> 
     public static XmlDocument XmlResponse 
     { 
      get { return xmlResponse; } 
     } 

     /// <summary> 
     /// Save the Stream representing the SOAP request 
     /// or SOAP response into a local memory buffer. 
     /// </summary> 
     /// <param name="stream"> 
     /// <returns></returns> 
     public override Stream ChainStream(Stream stream) 
     { 
      oldStream = stream; 
      newStream = new MemoryStream(); 
      return newStream; 
     } 

     /// <summary> 
     /// If the SoapMessageStage is such that the SoapRequest or 
     /// SoapResponse is still in the SOAP format to be sent or received, 
     /// save it to the xmlRequest or xmlResponse property. 
     /// </summary> 
     /// <param name="message"> 
     public override void ProcessMessage(SoapMessage message) 
     { 
      switch (message.Stage) 
      { 
       case SoapMessageStage.BeforeSerialize: 
        break; 
       case SoapMessageStage.AfterSerialize: 
        xmlRequest = GetSoapEnvelope(newStream); 
        CopyStream(newStream, oldStream); 
        break; 
       case SoapMessageStage.BeforeDeserialize: 
        CopyStream(oldStream, newStream); 
        xmlResponse = GetSoapEnvelope(newStream); 
        break; 
       case SoapMessageStage.AfterDeserialize: 
        break; 
      } 
     } 

     /// <summary> 
     /// Returns the XML representation of the Soap Envelope in the supplied stream. 
     /// Resets the position of stream to zero. 
     /// </summary> 
     /// <param name="stream"> 
     /// <returns></returns> 
     private XmlDocument GetSoapEnvelope(Stream stream) 
     { 
      XmlDocument xml = new XmlDocument(); 
      stream.Position = 0; 
      StreamReader reader = new StreamReader(stream); 
      xml.LoadXml(reader.ReadToEnd()); 
      stream.Position = 0; 
      return xml; 
     } 

     /// <summary> 
     /// Copies a stream. 
     /// </summary> 
     /// <param name="from"> 
     /// <param name="to"> 
     private void CopyStream(Stream from, Stream to) 
     { 
      TextReader reader = new StreamReader(from); 
      TextWriter writer = new StreamWriter(to); 
      writer.WriteLine(reader.ReadToEnd()); 
      writer.Flush(); 
     } 

     #region NoOp 
     /// <summary> 
     /// Included only because it must be implemented. 
     /// </summary> 
     /// <param name="methodInfo"> 
     /// <param name="attribute"> 
     /// <returns></returns> 
     public override object GetInitializer(LogicalMethodInfo methodInfo, 
      SoapExtensionAttribute attribute) 
     { 
      return null; 
     } 

     /// <summary> 
     /// Included only because it must be implemented. 
     /// </summary> 
     /// <param name="WebServiceType"> 
     /// <returns></returns> 
     public override object GetInitializer(Type WebServiceType) 
     { 
      return null; 
     } 

     /// <summary> 
     /// Included only because it must be implemented. 
     /// </summary> 
     /// <param name="initializer"> 
     public override void Initialize(object initializer) 
     { 
     } 
     #endregion NoOp 
    } 
}