2009-03-04 43 views
1

我有一個WCF客戶端與我無法控制的未知服務器實現進行通信。這個客戶端工作正常,它只是不喜歡,似乎是錯誤地形成的SOAP錯誤消息。我收到的郵件是這樣的:可能消耗嚴重形成的故障消息?

 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Header>...</soap:Header> 
    <soap:Body> 
     <soap:Fault> 
      <soap:faultcode>soap:Client</soap:faultcode> 
      <soap:faultstring>...</soap:faultstring> 
      <soap:detail>...</soap:detail> 
     </soap:Fault> 
    </soap:Body> 
</soap:Envelope> 

我相信根據肥皂架構的子元素不應該有資格和Ned看起來像:

 
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Header>...</soap:Header> 
    <soap:Body> 
     <soap:Fault> 
      <faultcode>soap:Client</faultcode> 
      <faultstring>...</faultstring> 
      <detail>...</detail> 
     </soap:Fault> 
    </soap:Body> 
</soap:Envelope> 

有什麼我可以配置或覆蓋,以便我可以使用後一種格式的消息,以便我可以使用錯誤消息而不是xml異常?

回答

4

我不記得我是怎麼發現對面信息督察偶然,而是它如何我解決我的問題。

Thisthis文章創建檢查所提供的基礎,接下來是檢查的肉:

 
public void AfterReceiveReply(ref Message reply, object correlationState) 
{ 
    if (!reply.IsFault) 
     return; 

    var document = new XmlDocument(); 

    document.Load(reply.GetReaderAtBodyContents()); 

    var navigator = document.CreateNavigator(); 
    var manager = new XmlNamespaceManager(navigator.NameTable); 

    manager.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/"); 

    var it = navigator.Select("//soap:Fault", manager); 

    if (it.MoveNext() && it.Current.HasChildren && it.Current.MoveToChild(XPathNodeType.Element)) 
    { 
     do 
     { 
      var c = it.Current; 

      if (string.IsNullOrEmpty(c.Prefix)) 
       continue; 

      c.ReplaceSelf("<" + c.LocalName + ">" + c.InnerXml + "</" + c.LocalName + ">"); 

      /// we may want to record the detail included inside the detail element, 
      /// it is not reported in the FaultException that is raised. 

     } while (it.Current.MoveToNext()); 
    } 

    var reader = XmlDictionaryReader.CreateDictionaryReader(new XmlNodeReader(document)); 

    reader.MoveToStartElement(); 

    var fixedReply = Message.CreateMessage(reply.Version, null, reader); 

    fixedReply.Headers.CopyHeadersFrom(reply.Headers); 
    fixedReply.Properties.CopyProperties(reply.Properties); 

    reply = fixedReply; 
} 

0

看起來有問題的應用程序使用自定義(和嚴重實施)SOAP庫。下面的文章可能會有所幫助(我還沒有處理過這個問題,因爲我在一個純粹的.Net商店中)。

http://msdn.microsoft.com/en-us/library/ms733721.aspx

+0

也許我錯過了什麼,但我相信這是所有的東西,我可以爲服務器做,而不是客戶端。糾正我,如果我錯了,但似乎可以創建一個IErrorHandler實現,但我相信這隻適用於服務器端。 – Dave 2009-03-04 17:39:16

0

注意,System.Web.Services.Protocols.SoapHttpClientProtocol類似乎顯著比WCF更容忍格式錯誤的響應。

這有時被稱爲ASMX服務協議。這也是一個可以考慮的選項。

霍華德·霍夫曼

0
} catch (SoapFaultClientException e) { 
    log.error(e); 
    SoapFaultDetail soapFaultDetail = e.getSoapFault().getFaultDetail(); 
    SoapFaultDetailElement detailElementChild = (SoapFaultDetailElement) soapFaultDetail.getDetailEntries().next(); 
    Source detailSource = detailElementChild.getSource(); 

    try { 
     Object detail = (JAXBElement<SearchResponse>) getWebServiceTemplate().getUnmarshaller().unmarshal(detailSource); 
//    throw new SoapFaultWithDetailException(detail); 

    } catch (IOException e1) { 
     throw new IllegalArgumentException("cannot unmarshal SOAP fault detail object: " + soapFaultDetail.getSource()); 
    } 

} 
+0

請至少對您的代碼進行一些解釋。 – 2015-09-16 13:54:41