2016-08-24 73 views
1

我被給了一個wsdl文件,我在Visual Studio中添加它作爲服務引用。C#如何從服務引用(wsdl導入)獲取soap XML?

我現在可以創建一個新的服務客戶端,然後做肥皂行動沒有問題。

但是在C#中,我只需要抓取它生成的soap請求XML來執行操作並將其存儲在變量中,而不是實際發送給Web服務。

這可能嗎?我試着將這個類序列化爲xml,但結果與預期的soap請求xml有點不同。

+1

就像您創建的服務客戶端一樣,也使用wsdl創建web服務。然後使用SoapUI獲取Web服務的soap請求。 –

+0

@KosalaW感謝您的幫助。我沒有考慮用它創建我自己的Web服務。但是,我確實需要以編程方式獲得它,所以我認爲我需要的不僅僅是SoapUI。但是,也許我創建的Web服務可能只是返回請求XML,如果沒有其他方式... – Kyle

+0

只是爲了確認我的理解你的要求,你有一個WSDL,你想知道,什麼是SOAP請求(XML?)對於您計劃實施的每個操作而言都會看起來像? –

回答

0

我仍在尋找這個,但最好的選擇是創建一個Web服務引用的部分類。

在Visual Studio解決方案資源管理器中(選中您的項目),單擊顯示所有文件圖標,這將允許您展開Web服務,直到您到達名爲Reference.cs(位於Reference.map節點下)的文件。

這個類將被標記爲partialSoapHttpClientProtocol繼承這應該給你訪問一些有趣的屬性和方法等

當我完成探索稍後我會更新這個答案。

希望它有幫助!

更新; 我發現這個有用的文章,我認爲將解決問題;

http://orbinary.com/blog/2010/01/getting-the-raw-soap-xml-sent-via-soaphttpclientprotocol/

您可以使用下面的類來實現XmlWriterSpy;

public class XmlWriterSpy : XmlWriter 
{ 
    private readonly XmlWriter _originalWriter; 
    private readonly XmlTextWriter _buffer; 
    private readonly StringWriter _sw; 

    public XmlWriterSpy(XmlWriter originalWriter) 
    { 
     _originalWriter = originalWriter; 

     _sw = new StringWriter(); 

     _buffer = new XmlTextWriter(_sw) 
     { 
      Formatting = Formatting.Indented 
     }; 
    } 

    public override void Flush() 
    { 
     _originalWriter.Flush(); 
     _buffer.Flush(); 
     _sw.Flush(); 
    } 

    public string Xml => _sw?.ToString(); 

    public override WriteState WriteState => _originalWriter.WriteState; 

    public override void Close() { _originalWriter.Close(); _buffer.Close(); } 

    public override string LookupPrefix(string ns) { return _originalWriter.LookupPrefix(ns); } 

    public override void WriteRaw(string data) 
    { 
     _originalWriter.WriteRaw(data); 
     _buffer.WriteRaw(data); 
    } 

    public override void WriteBase64(byte[] buffer, int index, int count) 
    { 
     _originalWriter.WriteBase64(buffer, index, count); 
     _buffer.WriteBase64(buffer, index, count); 
    } 


    public override void WriteString(string text) 
    { 
     _originalWriter.WriteString(text); 
     _buffer.WriteString(text); 
    } 

    public override void WriteSurrogateCharEntity(char lowChar, char highChar) 
    { 
     _originalWriter.WriteSurrogateCharEntity(lowChar, highChar); 
     _buffer.WriteSurrogateCharEntity(lowChar, highChar); 
    } 
    public override void WriteChars(char[] buffer, int index, int count) 
    { 
     _originalWriter.WriteChars(buffer, index, count); 
     _buffer.WriteChars(buffer, index, count); 
    } 

    public override void WriteRaw(char[] buffer, int index, int count) 
    { 
     _originalWriter.WriteRaw(buffer, index, count); 
     _buffer.WriteRaw(buffer, index, count); 
    } 

    public override void WriteStartDocument() 
    { 
     _originalWriter.WriteStartDocument(); 
     _buffer.WriteStartDocument(); 
    } 

    public override void WriteStartDocument(bool standalone) 
    { 
     _originalWriter.WriteStartDocument(standalone); 
     _buffer.WriteStartDocument(standalone); 
    } 

    public override void WriteEndDocument() 
    { 
     _originalWriter.WriteEndDocument(); 
     _buffer.WriteEndDocument(); 
    } 

    public override void WriteDocType(string name, string pubid, string sysid, string subset) 
    { 
     _originalWriter.WriteDocType(name, pubid, sysid, subset); 
     _buffer.WriteDocType(name, pubid, sysid, subset); 
    } 

    public override void WriteStartElement(string prefix, string localName, string ns) 
    { 
     _originalWriter.WriteStartElement(prefix, localName, ns); 
     _buffer.WriteStartElement(prefix, localName, ns); 
    } 

    public override void WriteEndElement() 
    { 
     _originalWriter.WriteEndElement(); 
     _buffer.WriteEndElement(); 
    } 

    public override void WriteFullEndElement() 
    { 
     _originalWriter.WriteFullEndElement(); 
     _buffer.WriteFullEndElement(); 
    } 

    public override void WriteStartAttribute(string prefix, string localName, string ns) 
    { 
     _originalWriter.WriteStartAttribute(prefix, localName, ns); 
     _buffer.WriteStartAttribute(prefix, localName, ns); 
    } 

    public override void WriteEndAttribute() 
    { 
     _originalWriter.WriteEndAttribute(); 
     _buffer.WriteEndAttribute(); 
    } 

    public override void WriteCData(string text) 
    { 
     _originalWriter.WriteCData(text); 
     _buffer.WriteCData(text); 
    } 

    public override void WriteComment(string text) 
    { 
     _originalWriter.WriteComment(text); 
     _buffer.WriteComment(text); 
    } 

    public override void WriteProcessingInstruction(string name, string text) 
    { 
     _originalWriter.WriteProcessingInstruction(name, text); 
     _buffer.WriteProcessingInstruction(name, text); 
    } 

    public override void WriteEntityRef(string name) 
    { 
     _originalWriter.WriteEntityRef(name); 
     _buffer.WriteEntityRef(name); 
    } 

    public override void WriteCharEntity(char ch) 
    { 
     _originalWriter.WriteCharEntity(ch); 
     _buffer.WriteCharEntity(ch); 
    } 

    public override void WriteWhitespace(string ws) 
    { 
     _originalWriter.WriteWhitespace(ws); 
     _buffer.WriteWhitespace(ws); 
    } 
} 

然後你只需要繼承代理生成的類,如下所示;

public class MyHelloService : HelloService 
{ 

    private XmlWriterSpy _xmlSpy; 

    public MyHelloService() : base() { } 

    protected override XmlWriter GetWriterForMessage(SoapClientMessage message, int bufferSize) 
    { 
     _xmlSpy = new XmlWriterSpy(base.GetWriterForMessage(message, bufferSize)); 
     return _xmlSpy; 
    } 

    public string Xml => _xmlSpy?.Xml; 
}