2010-11-04 85 views
2

在我的應用程序中,我正在調用Web服務,並且通過使用SoapExtension和SoapExtensionAttribute,我可以攔截傳入和傳出的SOAP消息以進行日誌記錄。我使用http://msdn.microsoft.com/en-us/magazine/cc164007.aspx中的示例作爲輸入。 但現在我想更進一步。我有一個Windows客戶端調用我的類(在一個單獨的項目中),然後類調用Web服務。我現在可以攔截SOAP消息,但不是直接將它們記錄到文件中,而是想將這些消息傳回給我的類,該類調用Web服務,並返回給調用我的類的客戶端。這是迄今爲止我所做的代碼更改:將信息傳遞給在其上應用SoapExtensionAttribute的對象

 private String ExtractFromStream(Stream target) 
    { 
     if (target != null) 
      return (new StreamReader(target)).ReadToEnd(); 

     return ""; 
    } 

    public void WriteOutput(SoapMessage message) 
    { 
     newStream.Position = 0; 
     string soapOutput = ExtractFromStream(newStream); 

     newStream.Position = 0; 
     Copy(newStream, oldStream); 
    } 

    public void WriteInput(SoapMessage message) 
    { 
     Copy(oldStream, newStream); 

     newStream.Position = 0; 
     string soapInput= ExtractFromStream(newStream); 
     newStream.Position = 0; 
    } 

我現在想通過soapInput和soapOutput回到那個認爲,這個屬性被應用在方法的類。我應該怎麼做的任何線索?

回答

2

對於任何人出現這種情況路過,這裏是解決方案:

的SOAPMessage對象不包含關於我的客戶的任何信息。但是,我可以將此對象轉換爲SoapClientMessage對象,然後我將訪問我的Web服務。如果我現在添加一個方法來此WebService(通過創建一個新的公共部分類),我可以(純屬一個例子!)訪問其屬性和這樣的方法:

private String ExtractFromStream(Stream target) 
    { 
     if (target != null) 
      return (new StreamReader(target)).ReadToEnd(); 

     return ""; 
    } 



    public void WriteOutput(SoapMessage message) 
    { 
     newStream.Position = 0; 

     string soapOutput = ExtractFromStream(newStream); 
     SoapClientMessage soapClient = (SoapClientMessage)message; 
     WebServiceClass webservice = (WebServiceClass)soapClient.Client; 
     webservice.MyMethod(soapOutput); //Use your own method here! 


     newStream.Position = 0; 
     Copy(newStream, oldStream); 
    } 

    public void WriteInput(SoapMessage message) 
    { 
     Copy(oldStream, newStream);   
     newStream.Position = 0; 

     string soapInput= ExtractFromStream(newStream); 
     SoapClientMessage soapClient = (SoapClientMessage)message; 
     WebServiceClass webservice = (WebServiceClass)soapClient.Client; 
     webservice.MyMethod(soapInput); 
     newStream.Position = 0; 
    } 

您可以添加方法(如的MyMethod中這個例子)通過創建一個新的公共部分類並添加方法,屬性和任何你喜歡的東西給你的WebServiceClass。