title: ASP.Net Web Service - What is the correct way to add a detail element to a SOAP fault response that works for both SOAP 1.1 and SOAP 1.2?
body: <p>ASP.Net 2.0 Web Services automatically create both SOAP 1.1 and SOAP 1.2 bindings. Our web service, however, has SOAP extensions and custom exception handling that make the assumption that only the SOAP 1.1 binding is used (for example, the SOAP extension uses the HTTP SOAPAction header to control behavior).</p>
  
  <p>I am looking to correct the code that makes these assumptions and make it work with either SOAP 1.1 or SOAP 1.2 properly. I am running into a bit of a problem in the generation of  elements for our SOAP faults.</p>
  
  <p>Consider the following web method implementation:</p>
  
  <pre><code>[WebMethod]
  public void
  ThrowsSoapException()
  {
      throw new SoapException("This is a SOAP exception.", SoapException.ServerFaultCode);
  }
  </code></pre>
  
  <p>Invoking this via SOAP 1.1 yields the following result:</p>
  
  <pre><code>&lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
     &lt;soap:Body&gt;
        &lt;soap:Fault&gt;
           &lt;faultcode&gt;soap:Server&lt;/faultcode&gt;
           &lt;faultstring&gt;This is a SOAP exception.&lt;/faultstring&gt;
           &lt;detail/&gt;
        &lt;/soap:Fault&gt;
     &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>Invoking via SOAP 1.2 yields the following result:</p>
  
  <pre><code>&lt;soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
     &lt;soap:Body&gt;
        &lt;soap:Fault&gt;
           &lt;soap:Code&gt;
              &lt;soap:Value&gt;soap:Receiver&lt;/soap:Value&gt;
           &lt;/soap:Code&gt;
           &lt;soap:Reason&gt;
              &lt;soap:Text xml:lang="en"&gt;This is a SOAP exception.&lt;/soap:Text&gt;
           &lt;/soap:Reason&gt;
           &lt;soap:Detail/&gt;
        &lt;/soap:Fault&gt;
     &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>In both these cases there is an empty detail element as a child of the <code>&lt;soap:Fault&gt;</code> element, but it has a different qualified name, either <code>&lt;detail&gt;</code> or <code>&lt;soap:Detail&gt;</code>.</p>
  
  <p>Now consider the following code that tries to create a SOAPException with a detail element.</p>
  
  <pre><code>[WebMethod]
  public void
  ThrowsSoapExceptionWithDetail()
  {
      XmlDocument doc = new XmlDocument();
      XmlNode detail =
          doc.CreateNode(XmlNodeType.Element, SoapException.DetailElementName.Name, SoapException.DetailElementName.Namespace);
      XmlNode custom =
          doc.CreateNode(XmlNodeType.Element, "custom", "http://example.com/xml/namespace/blah");
      custom.InnerXml = "Detail value";
      detail.AppendChild(custom);
      throw new SoapException("This is a SOAP exception with a detail element.", SoapException.ServerFaultCode, Context.Request.Url.AbsoluteUri, detail);
  }
  </code></pre>
  
  <p>The SOAP 1.1 response is:</p>
  
  <pre><code>&lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
     &lt;soap:Body&gt;
        &lt;soap:Fault&gt;
           &lt;faultcode&gt;soap:Server&lt;/faultcode&gt;
           &lt;faultstring&gt;This is a SOAP exception with a detail element.&lt;/faultstring&gt;
           &lt;faultactor&gt;http://localhost/simplewebservice/service1.asmx&lt;/faultactor&gt;
           &lt;detail&gt;
              &lt;custom xmlns="http://example.com/xml/namespace/blah"&gt;Detail value&lt;/custom&gt;
           &lt;/detail&gt;
        &lt;/soap:Fault&gt;
     &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>and the SOAP 1.2 response is:</p>
  
  <pre><code>&lt;soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
     &lt;soap:Body&gt;
        &lt;soap:Fault&gt;
           &lt;soap:Code&gt;
              &lt;soap:Value&gt;soap:Receiver&lt;/soap:Value&gt;
           &lt;/soap:Code&gt;
           &lt;soap:Reason&gt;
              &lt;soap:Text xml:lang="en"&gt;This is a SOAP exception with a detail element.&lt;/soap:Text&gt;
           &lt;/soap:Reason&gt;
           &lt;soap:Node&gt;http://localhost/simplewebservice/service1.asmx&lt;/soap:Node&gt;
           &lt;detail&gt;
              &lt;custom xmlns="http://example.com/xml/namespace/blah"&gt;Detail value&lt;/custom&gt;
           &lt;/detail&gt;
        &lt;/soap:Fault&gt;
     &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>
  
  <p>The SOAP 1.2 response now has the wrong qualified name for the detail element. It should be <code>&lt;soap:Detail&gt;</code>, but instead is merely <code>&lt;detail&gt;</code>, same as the SOAP 1.1 response.</p>
  
  <p>It seems that the ASP.Net 2.0 framework has done quite a bit to transform a SOAPException into the appropriate form for the SOAP version, but neglected to properly handle the detail element. Additionally, they don't seem to have exposed the correct SOAP 1.2 qualified name for the detail element as was done with the SoapException.DetailElementName property.</p>
  
  <p>So, what is the correct way to add a detail element to a SOAP fault response that works for both SOAP 1.1 and SOAP 1.2? Do I need to detect the SOAP version myself and hard-code the SOAP 1.2 qualified name for the detail element?</p>