Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- throw new FaultException<FaultDetails>(
- new FaultDetails("Throwing FaultException<FaultDetails>."),
- new FaultReason("Testing fault exceptions."),
- FaultCode.CreateSenderFaultCode(new FaultCode("MySubFaultCode"))
- );
- <?xml version="1.0" encoding="utf-16"?>
- <Fault xmlns="http://www.w3.org/2003/05/soap-envelope">
- <Code>
- <Value>Sender</Value>
- <Subcode>
- <Value>MySubFaultCode</Value>
- </Subcode>
- </Code>
- <Reason>
- <Text xml:lang="en-US">Testing fault exceptions.</Text>
- </Reason>
- <Detail>
- <FaultDetails xmlns="http://schemas.datacontract.org/2004/07/ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
- <Message>Throwing FaultException<FaultDetails>.</Message>
- </FaultDetails>
- </Detail>
- <?xml version="1.0" encoding="utf-16"?>
- <s:Fault xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
- <faultcode>s:MySubFaultCode</faultcode>
- <faultstring xml:lang="en-US">Testing fault exceptions.</faultstring>
- <detail>
- <FaultDetails xmlns="http://schemas.datacontract.org/2004/07/ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
- <Message>Throwing FaultException<FaultDetails>.</Message>
- </FaultDetails>
- </detail>
- </s:Fault>
- /// <summary>
- /// Replacement for the static methods on FaultCode to generate Sender and Receiver fault codes due
- /// to what seems like bugs in the implementation for basicHttpBinding (SOAP 1.1). wsHttpBinding
- /// (SOAP 1.2) seems to work just fine.
- ///
- /// The subCode parameter for FaultCode.CreateReceiverFaultCode and FaultCode.CreateSenderFaultCode
- /// seem to take over the main 'faultcode' value in the SOAP 1.1 response, whereas in SOAP 1.2 the
- /// subCode is correctly put under the 'Code->SubCode->Value' value in the XML response.
- ///
- /// This workaround is to create the FaultCode with Sender/Receiver (SOAP 1.2 terms, but gets
- /// translated by WCF depending on the binding) and an agnostic namespace found by using reflector
- /// on the FaultCode class. When that NS is passed in WCF seems to be able to generate the proper
- /// response with SOAP 1.1 (Client/Server) and SOAP 1.2 (Sender/Receiver) fault codes automatically.
- ///
- /// This means that it is not possible to create a FaultCode that works in both bindings with
- /// subcodes.
- /// </summary>
- /// <remarks>
- /// See http://stackoverflow.com/questions/65008/net-wcf-faults-generating-incorrect-soap-11-faultcode-values
- /// for more details.
- /// </remarks>
- public static class FaultCodeFactory
- {
- private const string _ns = "http://schemas.microsoft.com/ws/2005/05/envelope/none";
- /// <summary>
- /// Creates a sender fault code.
- /// </summary>
- /// <returns>A FaultCode object.</returns>
- /// <remarks>Does not support subcodes due to a WCF bug.</remarks>
- public static FaultCode CreateSenderFaultCode()
- {
- return new FaultCode("Sender", _ns);
- }
- /// <summary>
- /// Creates a receiver fault code.
- /// </summary>
- /// <returns>A FaultCode object.</returns>
- /// <remarks>Does not support subcodes due to a WCF bug.</remarks>
- public static FaultCode CreateReceiverFaultCode()
- {
- return new FaultCode("Receiver", _ns);
- }
- }
Add Comment
Please, Sign In to add comment