Windows Communication Foundation - SPLessons

WCF Exception handling

Home > Lesson > Chapter 18
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

WCF Exception handling

WCF Exception Handling

shape Description

Exception handling is used to throw the errors with proper manner. In WCF, fault contract is strongly recommended. It is associated with a service operation to denote the errors that can be a server or client. An operation can have one or more fault contracts associated with it. By default, these are SOAP (simple object access process) faults.

shape Syntax

try { // Write your code hare } catch (Exception ex) { throw ex; }

shape Example

Example to show the exceptions in client side. A simple WCF Service is created with a division of two numbers and write a following into IService.cs file like below: [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServiceLibraryexp { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. [ServiceContract] public interface IService1 { [OperationContract] int divide(int num1, int num2); } } [/csharp] Write the following code on Service1.cs as like below: [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServiceLibraryexp { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together. public class Service1 : IService1 { public int divide(int num1, int num2) { try { return num1 / num2; } catch(DivideByZeroException error) { throw error; } } } } } [/csharp] Now build the WCF Service for getting assembly file (.dll). Now, a simple WCF Service is obtained and one need to self-host the WCF Service.For that, add one project with the console application. Add service references like WCF Service reference and System.ServiceModel assembly from the .NET Framework and write the following code on Program.cs file like below: [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceModel; namespace Hosting { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(WcfServiceLibraryexp.Service1))) { host.Open(); Console.WriteLine("Host started time :" + DateTime.Now); Console.ReadLine(); } } } } [/csharp] Now build the entire solution and set the Console application as a start-up project and run the application.After running the application the result will be like below. WCF Service is successfully hosted and it is running successfully. Now create a client application with the Console application and add a service reference with the WCF Service which is currently running successfully. Then write the following code on a program.cs file consuming the service. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ServiceModel; namespace Clientconsumexp { class Program { static void Main(string[] args) { ServiceReference.Service1Client client = new ServiceReference.Service1Client(); try { client.divide(10, 0); } catch(Exception ex) { Console.WriteLine(ex.Message.ToString()); Console.ReadLine(); } } } } [/csharp] Run the client application, below screen appears.
After running the client application, one should get an exception called as divide by zero exception because zero values are passed to rise the exception.  So by this example, one cannot figure out what is the exact error. It is just says an internal error occurs, but the client cannot understand what the exact problem was.

shape More Info

Actually, WCF Service clients cannot see the errors like above. Because WCF Service communicates with the SOAP, which is nothing but XML. WCF service always communicates with the client using XML Standards or XML SOAP Standards.
In other words, even if needed to raise an exception that also must be in XML Format. Even using a normal.NET object exception through out an error, this cannot be sent in XML Format. So for that use something called as Fault Exception. In simple words, cannot use normal.NET exception object over here.

shape Syntax

Syntax of Fault Exception: try { // Write your code hare } catch (Exception ex) { throw new FaultException(ex.Message); }

shape Example

Now, go to WCF Service and add fault exception like below. Then write the following code in Service1.cs file. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WcfServiceLibraryexp { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together. public class Service1 : IService1 { public int divide(int num1, int num2) { try { return num1 / num2; } catch(DivideByZeroException error) { throw new FaultException(error.Message.ToString()); } } } } [/csharp] After, build the WCF Service and run the “Hosting” project. Then go to a client application and update the service which is reference fine in a client application. Then run the client application,the result appears like below.
The above screen says clearly that  a try was given to divide by zero. Here client can easily understand what the error is.