title: How to accept a SoapException on the client side
body: <p>I was getting the dreaded "client found response content type of "text/html" but expected "test/xml" error, but after some googling I found <a href="http://www.codinghorror.com/blog/2004/08/throwing-better-soap-exceptions.html" rel="nofollow">this post</a> about how to return a SoapException.</p>
  
  <p>So I went ahead and implemented that, but now I'm still getting the same error. Are there changes that I need to make on the client-side to be able to accept the SoapException (I wouldn't think so, everything just inherits from Exception, right?)</p>
  
  <p>Why am I still getting that dreaded response on the client side?</p>
  
  <pre><code>    [WebMethod]
      [SoapHeader("authenticationHeader")]
      [WebServiceAuthentication(AuthenticationRequired = false)]
      public DataSet GetConversionErrors()
      {
          try
          {
              return Components.PWD.GetConversionErrors();
          }
          catch (Exception ex)
          {
              PublishingManager.Publish(ex, HttpContext.Current.User.Identity.Name);
              throw Components.SoapException.GenerateSoapException(ex, Context);
          }
      }
  </code></pre>
  
  <p>Edit: I know that the exception in this catch block is exactly what I need to see, but I'm still getting the System.InvalidOperationException on the client-side.</p>
  
  <p>Edit2: I'm connecting to the webservices via a WebReference (calling a *.asmx file) if that helps/matters.</p>
  
  <p>Edit3: Here's what's getting written to our exceptionlog server-side (in the PublishingManager.Publish() call). My thought was that this should be what is returned to the client...</p>
  
  <pre><code>Exception Details:
  Exception Type: System.Exception
  Message: Test the text/html text/xml message. 
  Method: PWD.Components.PWD.GetConversionErrors 
  Username: xxxxxx Created 5/22/2012 4:20:53 PM 
  Application: xxxx Application Identity: NT AUTHORITY\IUSR 
  Source: xxxx Severity 2 
  Machine: xxxxxxxxx IP Address 127.0.0.1 
  AppDomain /LM/W3SVC/1/ROOT-1-129821937733610609 
  Stack Trace    at PWD.Components.PWD.GetConversionErrors() in C:\Vault\Development-New\Web\Main_Logging\PWD\Components\PWD.cs:line 73 
     at PWD.PWD.GetConversionErrors() in C:\Vault\Development-New\Web\Main_Logging\PWD\PWD.asmx.cs:line 44 
  Additional Information:
    +System.Exception: 
      Message: Test the text/html text/xml message. 
      Data: System.Collections.ListDictionaryInternal 
      InnerException:  
      TargetSite: System.Data.DataSet GetConversionErrors() 
      StackTrace:    at PWD.Components.PWD.GetConversionErrors() in C:\Vault\Development-New\Web\Main_Logging\PWD\Components\PWD.cs:line 73 
     at PWD.PWD.GetConversionErrors() in C:\Vault\Development-New\Web\Main_Logging\PWD\PWD.asmx.cs:line 44 
      HelpLink:  
      Source: xxxx 
       UserName: xxxxxxx 
       TypeName: PublishingManager 
       MachineName: xxxxxxxxx 
       TimeStamp: 5/22/2012 4:20:54 PM 
       FullName: Services.Publishing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=null 
       AppDomainName: /LM/W3SVC/1/ROOT-1-129821937733610609 
       ThreadIdentity:  
       WindowsIdentity: NT AUTHORITY\IUSR 
       Severity: 2 
       MachineIPAddress: 127.0.0.1 
  </code></pre>
  
  <p>Edit3: Just want to throw this extra code chunk so people see how I'm testing these exceptions (and maybe it's the wrong way to test...). In my first code chunk, I have the call inside the try block to "return Components.PWD.GetConversionErrors();", here's that method:</p>
  
  <pre><code>    public static DataSet GetConversionErrors()
      {
          DB db = new DB();
  
          try
          {
              //return db.ExecuteDataset(Configuration.GenericConfig.AppConnectionString, CommandType.Text, spGetConversionErrors);
              throw new Exception("Test the text/html text/xml message.");
          }
          catch
          {
              throw;
          }
      }
  </code></pre>
  
  <p>As you can see, I've commented out the actual call, and just threw in my own line that will throw an exception everytime for testing purposes...</p>
  
  <p>Edit4: @gbvb Here's the new method:</p>
  
  <pre><code>    [WebMethod]
      [SoapHeader("authenticationHeader")]
      [WebServiceAuthentication(AuthenticationRequired = false)]
      public DataSet GetConversionErrors()
      {
          try
          {
              return Components.PWD.GetConversionErrors();
          }
          catch (Exception ex)
          {
              //PublishingManager.Publish(ex, HttpContext.Current.User.Identity.Name);
              //throw Components.SoapException.GenerateSoapException(ex, Context);
  
              System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
              System.Xml.XmlNode detailNode = xmlDoc.CreateNode(System.Xml.XmlNodeType.Element, System.Web.Services.Protocols.SoapException.DetailElementName.Name, System.Web.Services.Protocols.SoapException.DetailElementName.Namespace);
              detailNode.InnerText = ex.ToString();
  
              throw new System.Web.Services.Protocols.SoapException(ex.Message, System.Web.Services.Protocols.SoapException.ClientFaultCode, System.Web.HttpContext.Current.Request.Url.AbsoluteUri, detailNode, ex.InnerException);
          }
      }
  </code></pre>
  
  <p>Edit5: What Fiddler2 saw:</p>
  
  <pre><code>HTTP/1.1 500 Internal Server Error
  Cache-Control: private
  Content-Type: text/html
  Server: Microsoft-IIS/7.5
  X-Powered-By: ASP.NET
  Date: Wed, 23 May 2012 19:25:56 GMT
  Content-Length: 1208
  
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/&gt;
  &lt;title&gt;500 - Internal server error.&lt;/title&gt;
  &lt;style type="text/css"&gt;
  &lt;!--
  body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
  fieldset{padding:0 15px 10px 15px;} 
  h1{font-size:2.4em;margin:0;color:#FFF;}
  h2{font-size:1.7em;margin:0;color:#CC0000;} 
  h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
  #header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
  background-color:#555555;}
  #content{margin:0 0 0 2%;position:relative;}
  .content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
  --&gt;
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="header"&gt;&lt;h1&gt;Server Error&lt;/h1&gt;&lt;/div&gt;
  &lt;div id="content"&gt;
   &lt;div class="content-container"&gt;&lt;fieldset&gt;
    &lt;h2&gt;500 - Internal server error.&lt;/h2&gt;
    &lt;h3&gt;There is a problem with the resource you are looking for, and it cannot be displayed.&lt;/h3&gt;
   &lt;/fieldset&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>If anyone knows out to nicely format this section, please do :)</p>