Extracting detail from a WCF FaultException response

前端 未结 4 2490
慢半拍i
慢半拍i 2021-02-19 22:29

I am successfully working with a third party soap service. I have added a service reference to a soap web service which has auto generated the classes.

When an error oc

4条回答
  •  滥情空心
    2021-02-19 23:34

    Here's a few methods I've found of extracting that detailed exception information from FaultExceptions

    Get the String Contents of a Single Element

    catch (FaultException e)
    {
        var errorElement = XElement.Parse(e.CreateMessageFault().GetReaderAtDetailContents().ReadOuterXml());
        var errorDictionary = errorElement.Elements().ToDictionary(key => key.Name.LocalName, val => val.Value);
        var errorMessage = errorDictionary?["ErrorMessage"];
    }
    

    Example Output:

    Organization does not exist.

    Get the String Contents of a All Details as a Single String

    catch (FaultException e)
    {
        var errorElement = XElement.Parse(e.CreateMessageFault().GetReaderAtDetailContents().ReadOuterXml());
        var errorDictionary = errorElement.Elements().ToDictionary(key => key.Name.LocalName, val => val.Value);
        var errorDetails = string.Join(";", errorDictionary);
    }
    

    Example Output:

    [ErrorMessage, Organization does not exist.];[EventCode, 3459046134826139648];[Parameters, ]

    Get the String Contents of a Everything as an XML string

    var errorElement = XElement.Parse(e.CreateMessageFault().GetReaderAtDetailContents().ReadOuterXml());
    var xmlDetail = (string)errorElement;
    

    Example Output:

    
        Organization does not exist.
        3459046134826139648
        
    
    

提交回复
热议问题