<p>I am experimenting with using the FaultException and FaultException&lt;T&gt; to determine the best usage pattern in our applications. We need to support WCF as well as non-WCF service consumers/clients, including SOAP 1.1 and SOAP 1.2 clients.</p>

<p>FYI: using FaultExceptions with wsHttpBinding results in SOAP 1.2 semantics whereas using FaultExceptions with basicHttpBinding results in SOAP 1.1 semantics. </p>

<p>I am using the following code to throw a FaultException&lt;FaultDetails&gt;:</p>

<pre><code>  throw new FaultException&lt;FaultDetails&gt;(
      new FaultDetails("Throwing FaultException&lt;FaultDetails&gt;."),
      new FaultReason("Testing fault exceptions."),
      FaultCode.CreateSenderFaultCode(new FaultCode("MySubFaultCode"))
      );
</code></pre>

<p>The FaultDetails class is just a simple test class that contains a string "Message" property as you can see below.</p>

<p>When using wsHttpBinding the response is:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;Fault xmlns="http://www.w3.org/2003/05/soap-envelope"&gt;
&lt;Code&gt;
  &lt;Value&gt;Sender&lt;/Value&gt;
  &lt;Subcode&gt;
    &lt;Value&gt;MySubFaultCode&lt;/Value&gt;
  &lt;/Subcode&gt;
&lt;/Code&gt;
&lt;Reason&gt;
  &lt;Text xml:lang="en-US"&gt;Testing fault exceptions.&lt;/Text&gt;
&lt;/Reason&gt;
&lt;Detail&gt;
  &lt;FaultDetails xmlns="http://schemas.datacontract.org/2004/07/ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;Message&gt;Throwing FaultException&amp;lt;FaultDetails&amp;gt;.&lt;/Message&gt;
  &lt;/FaultDetails&gt;
&lt;/Detail&gt;
</code></pre>

<p></p>

<p>This looks right according to the SOAP 1.2 specs. The main/root “Code” is “Sender”, which has a “Subcode” of “MySubFaultCode”. If the service consumer/client is using WCF the FaultException on the client side also mimics the same structure, with the faultException.Code.Name being “Sender” and faultException.Code.SubCode.Name being “MySubFaultCode”.</p>

<p>When using basicHttpBinding the response is:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;s:Fault xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  &lt;faultcode&gt;s:MySubFaultCode&lt;/faultcode&gt;
  &lt;faultstring xml:lang="en-US"&gt;Testing fault exceptions.&lt;/faultstring&gt;
  &lt;detail&gt;
    &lt;FaultDetails xmlns="http://schemas.datacontract.org/2004/07/ClassLibrary" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;Message&gt;Throwing FaultException&amp;lt;FaultDetails&amp;gt;.&lt;/Message&gt;
    &lt;/FaultDetails&gt;
  &lt;/detail&gt;
&lt;/s:Fault&gt;
</code></pre>

<p>This does not look right. Looking at the SOAP 1.1 specs, I was expecting to see the “faultcode” to have a value of “s:Client.MySubFaultCode” when I use FaultCode.CreateSenderFaultCode(new FaultCode("MySubFaultCode")). Also a WCF client gets an incorrect structure. The faultException.Code.Name is “MySubFaultCode” instead of being “Sender”, and the faultException.Code.SubCode is null instead of faultException.Code.SubCode.Name being “MySubFaultCode”. Also, the faultException.Code.IsSenderFault is false.</p>

<p>Similar problem when using FaultCode.CreateReceiverFaultCode(new FaultCode("MySubFaultCode")):</p>

<ul>
<li>works as expected for SOAP 1.2</li>
<li>generates “s:MySubFaultCode” instead of “s:Server.MySubFaultCode” and the faultException.Code.IsReceiverFault is false for SOAP 1.1</li>
</ul>

<p>This item was also posted by someone else on <a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=669420&amp;SiteID=1" rel="nofollow noreferrer">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=669420&amp;SiteID=1</a> in 2006 and no one has answered it. I find it very hard to believe that no one has run into this, yet. </p>

<p>Here is someone else having a similar problem: <a href="http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3883110&amp;SiteID=1&amp;mode=1" rel="nofollow noreferrer">http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3883110&amp;SiteID=1&amp;mode=1</a></p>

<p>Microsoft Connect bug: <a href="https://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=367963" rel="nofollow noreferrer">https://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=367963</a></p>

<p>Description of how faults should work: <a href="http://blogs.msdn.com/drnick/archive/2006/12/19/creating-faults-part-3.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/drnick/archive/2006/12/19/creating-faults-part-3.aspx</a></p>

<p>Am I doing something wrong or is this truly a bug in WCF?</p>

[{'answer_id': 69390, 'author': 'wojo', 'author_id': 9022, 'author_profile': 'https://Stackoverflow.com/users/9022', 'pm_score': 4, 'selected': True, 'text': '<p>This is my current workaround:</p>\n\n<pre><code>    /// &lt;summary&gt;\n    /// Replacement for the static methods on FaultCode to generate Sender and Receiver fault codes due\n    /// to what seems like bugs in the implementation for basicHttpBinding (SOAP 1.1). wsHttpBinding \n    /// (SOAP 1.2) seems to work just fine.\n    /// \n    /// The subCode parameter for FaultCode.CreateReceiverFaultCode and FaultCode.CreateSenderFaultCode\n    /// seem to take over the main \'faultcode\' value in the SOAP 1.1 response, whereas in SOAP 1.2 the\n    /// subCode is correctly put under the \'Code-&gt;SubCode-&gt;Value\' value in the XML response.\n    /// \n    /// This workaround is to create the FaultCode with Sender/Receiver (SOAP 1.2 terms, but gets\n    /// translated by WCF depending on the binding) and an agnostic namespace found by using reflector\n    /// on the FaultCode class. When that NS is passed in WCF seems to be able to generate the proper\n    /// response with SOAP 1.1 (Client/Server) and SOAP 1.2 (Sender/Receiver) fault codes automatically.\n    /// \n    /// This means that it is not possible to create a FaultCode that works in both bindings with\n    /// subcodes.\n    /// &lt;/summary&gt;\n    /// &lt;remarks&gt;\n    /// See http://stackoverflow.com/questions/65008/net-wcf-faults-generating-incorrect-soap-11-faultcode-values\n    /// for more details.\n    /// &lt;/remarks&gt;\n    public static class FaultCodeFactory\n    {\n        private const string _ns = "http://schemas.microsoft.com/ws/2005/05/envelope/none";\n\n        /// &lt;summary&gt;\n        /// Creates a sender fault code.\n        /// &lt;/summary&gt;\n        /// &lt;returns&gt;A FaultCode object.&lt;/returns&gt;\n        /// &lt;remarks&gt;Does not support subcodes due to a WCF bug.&lt;/remarks&gt;\n        public static FaultCode CreateSenderFaultCode()\n        {\n            return new FaultCode("Sender", _ns);\n        }\n\n        /// &lt;summary&gt;\n        /// Creates a receiver fault code.\n        /// &lt;/summary&gt;\n        /// &lt;returns&gt;A FaultCode object.&lt;/returns&gt;\n        /// &lt;remarks&gt;Does not support subcodes due to a WCF bug.&lt;/remarks&gt;\n        public static FaultCode CreateReceiverFaultCode()\n        {\n            return new FaultCode("Receiver", _ns);\n        }\n    }\n</code></pre>\n\n<p>Sadly I don\'t see a way to use subcodes without breaking either SOAP 1.1 or 1.2 clients. </p>\n\n<p>If you use the Code.SubCode syntax, you can create SOAP 1.1 compatible faultcode values but it breaks SOAP 1.2.</p>\n\n<p>If you use the proper subcode support in .NET (either via the static FaultCode methods or one of the overloads) it breaks SOAP 1.1 but works in SOAP 1.2.</p>\n'}, {'answer_id': 206822, 'author': 'wojo', 'author_id': 9022, 'author_profile': 'https://Stackoverflow.com/users/9022', 'pm_score': 3, 'selected': False, 'text': '<p>Response from Microsoft:</p>\n\n<p>As discussed in <a href="http://msdn.microsoft.com/en-us/library/ms789039.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ms789039.aspx</a>, there are two methods outlined in the Soap 1.1 specification for custom fault codes:</p>\n\n<p>(1) Using the "dot" notation as you describe</p>\n\n<p>(2) Defining entirely new fault codes</p>\n\n<p>Unfortunately, the "dot" notation should be avoided, as it\'s use is discouraged in the WS-I Basic Profile specification. Essentially, this means that there is no real equivalent of the Soap 1.2 fault SubCode when using Soap 1.1.</p>\n\n<p>So, when generating faults, you\'ll have to be cognizant of the MessageVersion defined in the binding, and generate faultcodes accordingly.</p>\n\n<p>Since "sender" and "receiver" are not vaild fault codes for Soap 1.1, and there is no real equivalent of a fault subcode, you shouldn\'t use the CreateSenderFaultCode and CreateReceiverFaultCode methods when generating custom fault codes for Soap 1.1.</p>\n\n<p>Instead, you\'ll need to define your own faultcode, using your own namespace and name:</p>\n\n<p>FaultCode customFaultCode = new FaultCode(localName, faultNamespace);</p>\n'}]
['https://Stackoverflow.com/questions/65008', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9022/']