Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Part 16 - Soap faults in WCF

Suggested Videos
Part 13 - ExtensionDataObject in WCF
Part 14 - Risks of implementing IExtensibleDataObject interface
Part 15 - Exception handling in WCF



This is continuation to Part 15. Please watch Part 15, before proceeding. In Part 15, we have learnt that, WCF serializes exceptions to SOAP faults before reporting the exception information to the client. This is because exceptions are not allowed to be passed through a wcf service channel.



SOAP faults are in XML format and are platform independent. A typical SOAP fault contains
1. FaultCode
2. FaultReason 
3. Detail elements etc.

The Detail element can be used to include any custom xml. We will discuss more about Detail element in a later video session.

SOAP faults are formatted based on SOAP 1.1 or SOAP 1.2 speficications. SOAP format depends on the binding. BasicHttpBinding uses SOAP 1.1 whereas the other built-in WCF bindings use SOAP 1.2.

For the differences between SOAP 1.1 and 1.2 please refer to the following article.
http://www.w3.org/2003/06/soap11-soap12.html

The differences are not that important from a developer perspective, as WCF formats the messages automatically based on the binding we have used to expose the service.

To view SOAP Fault messages, please enable message logging in WCF. We have discussed enabling message logging in Part 9 of WCF video series.

To view SOAP 1.1 fault message, set binding to basicHttpBinding.

SOAP 1.1 Fault with includeExceptionDetailInFaults set to false:
<s:Envelope xmlns:s="http://...">
  <s:Body>
    <s:Fault>
      <faultcode xmlns:a="http://..." xmlns="">a:InternalServiceFault</faultcode>
      <faultstring xml:lang="en-GB" xmlns="http://...">
        The server was unable to process the request due to an internal error.
        For more information about the error, either turn on IncludeExceptionDetailInFaults
        (either from ServiceBehaviorAttribute or from the <serviceDebug>configuration behavior) 
        on the server in order to send the exception information back to the client, or turn 
        on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the 
        server trace logs.
      </faultstring>
    </s:Fault>
  </s:Body>
</s:Envelope>

SOAP 1.1 Fault with includeExceptionDetailInFaults set to true:
<s:Envelope xmlns:s="http://...">
  <s:Body>
    <s:Fault>
      <faultcode xmlns:a="http://..." xmlns="">a:InternalServiceFault</faultcode>
      <faultstring xml:lang="en-GB" xmlns="">Attempted to divide by zero.</faultstring>
      <detail xmlns="">
        <ExceptionDetail xmlns="http://..." xmlns:i="http://...">
          <HelpLink i:nil="true"></HelpLink>
          <InnerException i:nil="true"></InnerException>
          <Message>Attempted to divide by zero.</Message>
          <StackTrace>
            at CalculatorService.CalculatorService.Divide(Int32 Numerator, Int32 Denominator) 
            in C:\CalculatorService\CalculatorService\CalculatorService.cs:line 7
          </StackTrace>
          <Type>System.DivideByZeroException</Type>
        </ExceptionDetail>
      </detail>
    </s:Fault>
  </s:Body>
</s:Envelope>

To view SOAP 1.2 fault message, set binding to wsHttpBinding. By default Message Security is turned on for wsHttpBinding. Set the security mode for wsHttpBinding to None, so we could view the SOAP 1.2 fault message.
<bindings>
  <wsHttpBinding>
    <binding>
      <security mode="None">
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

SOAP 1.2 Fault with includeExceptionDetailInFaults set to false:
<s:Envelope xmlns:s="http://..." xmlns:a="http://...">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://...</a:Action>
    <a:RelatesTo>urn:uuid:3ea53f96-6e2f-48b3-83d8-ff81c8171153</a:RelatesTo>
  </s:Header>
  <s:Body>
    <s:Fault>
      <s:Code>
        <s:Value>s:Receiver</s:Value>
        <s:Subcode>
          <s:Value xmlns:a="http://...">a:InternalServiceFault</s:Value>
        </s:Subcode>
      </s:Code>
      <s:Reason>
        <s:Text xml:lang="en-GB">
          The server was unable to process the request due to an internal error.  
          For more information about the error, either turn on IncludeExceptionDetailInFaults 
          (either from ServiceBehaviorAttribute or from the <serviceDebug>configuration behavior) 
          on the server in order to send the exception information back to the client, or turn 
          on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.
        </s:Text>
      </s:Reason>
    </s:Fault>
  </s:Body>
</s:Envelope>

SOAP 1.2 Fault with includeExceptionDetailInFaults set to true:
<s:Envelope xmlns:s="http://..." xmlns:a="http://...">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://...</a:Action>
    <a:RelatesTo>urn:uuid:d649ab47-2813-4dd1-a97a-6ceea71c3e9c</a:RelatesTo>
  </s:Header>
  <s:Body>
    <s:Fault>
      <s:Code>
        <s:Value>s:Receiver</s:Value>
        <s:Subcode>
          <s:Value xmlns:a="http://...">a:InternalServiceFault</s:Value>
        </s:Subcode>
      </s:Code>
      <s:Reason>
        <s:Text xml:lang="en-GB">Attempted to divide by zero.</s:Text>
      </s:Reason>
      <s:Detail>
        <ExceptionDetail xmlns="http://..." xmlns:i="http://...">
          <HelpLink i:nil="true"></HelpLink>
          <InnerException i:nil="true"></InnerException>
          <Message>Attempted to divide by zero.</Message>
          <StackTrace>
            at CalculatorService.CalculatorService.Divide(Int32 Numerator, 
            Int32 Denominator) in C:\CalculatorService\CalculatorService
            \CalculatorService.cs:line 7 ...
          </StackTrace>
          <Type>System.DivideByZeroException</Type>
        </ExceptionDetail>
      </s:Detail>
    </s:Fault>
  </s:Body>
</s:Envelope>

wcf tutorial

2 comments:

  1. Dear Vankat
    A lot of Thanks of GOD!
    that you are perfect!!!
    sir i how many videos of WCF are left?
    and what is your next goal?

    ReplyDelete
  2. Hello Sir,when i try to attempt divide by 0 .it stops wcf sevice and open the wcf host webpage where it causes error.when i stop wcf service.it shows me the error like "An existing connection was forcibly closed by the remote host".Because of this i cant catch the message fault.

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.