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

Part 15 - Exception handling in WCF

Suggested Videos
Part 12 - Backward compatible WCF contract changes
Part 13 - ExtensionDataObject in WCF
Part 14 - Risks of implementing IExtensibleDataObject interface



When an exception occurs in a WCF service, the service serializes the exception into a SOAP fault, and then sends the SOAP fault to the client.

By default unhandled exception details are not included in SOAP faults that are propagated to client applications for security reasons. Instead a generic SOAP fault is returned to the client.



For debugging purpose, if you want to include exception details in SOAP faults, enable IncludeExceptionDetailInFaults setting. This can be done in 2 ways as shown below.
1. In the config file using service behavior configuration
<behaviors>
  <serviceBehaviors>
    <behavior name="inculdeExceptionDetails">
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

2. In code using ServiceBehavior attribute
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]
public class CalculatorService : ICalculatorService
{
    public int Divide(int Numerator, int Denominator)
    {
        return Numerator / Denominator;
    }
}

Frequently asked WCF interview questions
What happens when an exception occurs in a WCF service?
OR
What is a SOAP fault?
OR
How are WCF service exceptions reported to client applications?

Code used in the demo:
ICalculatorService.cs in CalculatorService project
using System.ServiceModel;
namespace CalculatorService
{
    [ServiceContract]
    public interface ICalculatorService
    {
        [OperationContract]
        int Divide(int Numerator, int Denominator);
    }
}

CalculatorService.cs in CalculatorService project
namespace CalculatorService
{
    public class CalculatorService : ICalculatorService
    {
        public int Divide(int Numerator, int Denominator)
        {
            return Numerator / Denominator;
        }
    }
}

App.config in CalculatorServiceHost project
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
  <services>
    <service name="CalculatorService.CalculatorService" 
              behaviorConfiguration="mexBehavior">
      <endpoint address="CalculatorService" binding="basicHttpBinding" 
                contract="CalculatorService.ICalculatorService">
      </endpoint>
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8080/"/>
        </baseAddresses>
      </host>
    </service>
  </services>
  <behaviors>
    <serviceBehaviors>
      <behavior name="mexBehavior">
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>
</configuration>

Program.cs in CalculatorServiceHost project
using System;
using System.ServiceModel;
namespace CalculatorServiceHost
{
    class Program
    {
        static void Main()
        {
            using (ServiceHost host = new ServiceHost
                (typeof(CalculatorService.CalculatorService)))
            {
                host.Open();
                Console.WriteLine("Host started @ " + DateTime.Now.ToString());
                Console.ReadLine();
            }
        }
    }
}

WebForm1.aspx in CalculatorClient project
<table style="font-family:Arial; border:1px solid black">
    <tr>
        <td>
            <b>Numerator:</b>
        </td>
        <td>
            <asp:TextBox ID="txtNumerator" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <b>Denominator:</b>
        </td>
        <td>
            <asp:TextBox ID="txtDenominator" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="btnDivide" runat="server" Text="Divide" Font-Bold="True"                                   onclick="btnDivide_Click" />
        </td>  
        <td>
            <asp:Label ID="lblResult" runat="server" Font-Bold="true">
            </asp:Label>
        </td>
    </tr>
</table>

WebForm1.aspx.cs in CalculatorClient project
protected void btnDivide_Click(object sender, EventArgs e)
{
    int numerator = Convert.ToInt32(txtNumerator.Text);
    int denominator = Convert.ToInt32(txtDenominator.Text);
    CalculatorService.CalculatorServiceClient client = 
        new CalculatorService.CalculatorServiceClient();
    lblResult.Text = client.Divide(numerator, denominator).ToString();
}

wcf tutorial

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. when im trying to attempt divide by 0 it raises an exception in wcf service.After i stop wcf service.it shows me the error like this "An existing connection was forcibly closed by the remote host ".i didnt get soap fault because of this process.every time it opens wcf service when i attempt divide by 0

    ReplyDelete
    Replies
    1. I got it by pressing F10

      Delete
  3. how to debugg wcf service hosted on IIS...

    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.