可能消耗错误形成的故障消息?

时间:2009-03-04 14:50:13

标签: soap wcf-client

我有一个WCF客户端与一个我无法控制的未知服务器实现进行通信。这个客户端工作得很好它只是不喜欢,似乎是错误形成的SOAP Fault消息。我收到的消息如下:

<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>  

我认为根据soap模式,子元素不应该被限定,并且看起来像:

<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 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

看起来违规应用程序正在使用自定义(并且执行不当)的SOAP库。以下文章可能有所帮助(我还没有处理过这个问题,因为我在一个纯粹的.Net商店)。

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

答案 2 :(得分:0)

请注意,System.Web.Services.Protocols.SoapHttpClientProtocol类似乎显着更容忍错误的错误响应而不是WCF。

这有时被称为ASMX服务协议。这也许是一个考虑的选择。

霍华德霍夫曼

答案 3 :(得分: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());
    }

}