code_junkie

.NET WCF faults generating incorrect SOAP 1.1 faultcode values

Nov 10th, 2011
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. throw new FaultException<FaultDetails>(
  2. new FaultDetails("Throwing FaultException<FaultDetails>."),
  3. new FaultReason("Testing fault exceptions."),
  4. FaultCode.CreateSenderFaultCode(new FaultCode("MySubFaultCode"))
  5. );
  6.  
  7. <?xml version="1.0" encoding="utf-16"?>
  8. <Fault xmlns="http://www.w3.org/2003/05/soap-envelope">
  9. <Code>
  10. <Value>Sender</Value>
  11. <Subcode>
  12. <Value>MySubFaultCode</Value>
  13. </Subcode>
  14. </Code>
  15. <Reason>
  16. <Text xml:lang="en-US">Testing fault exceptions.</Text>
  17. </Reason>
  18. <Detail>
  19. <FaultDetails xmlns="http://schemas.datacontract.org/2004/07/ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  20. <Message>Throwing FaultException<FaultDetails>.</Message>
  21. </FaultDetails>
  22. </Detail>
  23.  
  24. <?xml version="1.0" encoding="utf-16"?>
  25. <s:Fault xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  26. <faultcode>s:MySubFaultCode</faultcode>
  27. <faultstring xml:lang="en-US">Testing fault exceptions.</faultstring>
  28. <detail>
  29. <FaultDetails xmlns="http://schemas.datacontract.org/2004/07/ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  30. <Message>Throwing FaultException<FaultDetails>.</Message>
  31. </FaultDetails>
  32. </detail>
  33. </s:Fault>
  34.  
  35. /// <summary>
  36. /// Replacement for the static methods on FaultCode to generate Sender and Receiver fault codes due
  37. /// to what seems like bugs in the implementation for basicHttpBinding (SOAP 1.1). wsHttpBinding
  38. /// (SOAP 1.2) seems to work just fine.
  39. ///
  40. /// The subCode parameter for FaultCode.CreateReceiverFaultCode and FaultCode.CreateSenderFaultCode
  41. /// seem to take over the main 'faultcode' value in the SOAP 1.1 response, whereas in SOAP 1.2 the
  42. /// subCode is correctly put under the 'Code->SubCode->Value' value in the XML response.
  43. ///
  44. /// This workaround is to create the FaultCode with Sender/Receiver (SOAP 1.2 terms, but gets
  45. /// translated by WCF depending on the binding) and an agnostic namespace found by using reflector
  46. /// on the FaultCode class. When that NS is passed in WCF seems to be able to generate the proper
  47. /// response with SOAP 1.1 (Client/Server) and SOAP 1.2 (Sender/Receiver) fault codes automatically.
  48. ///
  49. /// This means that it is not possible to create a FaultCode that works in both bindings with
  50. /// subcodes.
  51. /// </summary>
  52. /// <remarks>
  53. /// See http://stackoverflow.com/questions/65008/net-wcf-faults-generating-incorrect-soap-11-faultcode-values
  54. /// for more details.
  55. /// </remarks>
  56. public static class FaultCodeFactory
  57. {
  58. private const string _ns = "http://schemas.microsoft.com/ws/2005/05/envelope/none";
  59.  
  60. /// <summary>
  61. /// Creates a sender fault code.
  62. /// </summary>
  63. /// <returns>A FaultCode object.</returns>
  64. /// <remarks>Does not support subcodes due to a WCF bug.</remarks>
  65. public static FaultCode CreateSenderFaultCode()
  66. {
  67. return new FaultCode("Sender", _ns);
  68. }
  69.  
  70. /// <summary>
  71. /// Creates a receiver fault code.
  72. /// </summary>
  73. /// <returns>A FaultCode object.</returns>
  74. /// <remarks>Does not support subcodes due to a WCF bug.</remarks>
  75. public static FaultCode CreateReceiverFaultCode()
  76. {
  77. return new FaultCode("Receiver", _ns);
  78. }
  79. }
Add Comment
Please, Sign In to add comment