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
The detail node of the message fault is expected to contain XML. The GetDetail will deserialize this XML into the given object.
As the contents is not XML it was possible to use this method.
You can however get access to the XML and read the innerXml value:
MessageFault msgFault = ex.CreateMessageFault();
var msg = msgFault.GetReaderAtDetailContents().Value;
This approached worked.
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
if (reply.IsFault)
{
// Create a copy of the original reply to allow default WCF processing
MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
Message copy = buffer.CreateMessage(); // Create a copy to work with
reply = buffer.CreateMessage(); // Restore the original message
MessageFault faultex = MessageFault.CreateFault(copy, Int32.MaxValue); //Get Fault from Message
FaultCode codigo = faultex.Code;
//if (faultex.HasDetail)... //More details
buffer.Close();
You can catch FaultException<TDetail>
, which gives you detail for free.
catch (FaultException<string> ex)
{
string yourDetail = ex.Detail;
}
Here's a few methods I've found of extracting that detailed exception information from FaultExceptions
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.
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, ]
var errorElement = XElement.Parse(e.CreateMessageFault().GetReaderAtDetailContents().ReadOuterXml());
var xmlDetail = (string)errorElement;
Example Output:
<FaultData xmlns="http://schemas.datacontract.org/2004/07/Xata.Ignition.Common.Contract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <ErrorMessage>Organization does not exist.</ErrorMessage> <EventCode>3459046134826139648</EventCode> <Parameters i:nil="true" xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays"></Parameters> </FaultData>