Understand WCF: Part 5 : Exception Handling in WCF Application

In this article series we are explaining WCF applications. Previous articles explained various important concepts of WCF applications. You can read the full article series here:

Understand WCF:Part-1:What is SOA, Services and Messages
Understand WCF: part-2: Understand RESTful Service 
Understand WCF: Part-3: Create simple WCF Service

Understand WCF: Part 4: What is Address, Binding, and Contract
In this article we will learn one very important concept of WCF applications; "How to handle exceptions in WCF applications". We know that an exception is a very common scenario in applications and it might occur anywhere and anytime. So, to develop a robust application, we need to prepare for the worst. In a normal native .NET application it's very easy to handle an exception because the entire code is in the same platform and a try-catch block is enough to handle them. We can catch the exception, log it or ignore it or do our custom exception.
But when we talk about SOA, things change a little bit. Because we are aware of our service but we are not aware of the client. We don't know in which platform the client is running and what its nature is.
Form a WCF service perspective; all the exceptions written in a WCF service can be consumed by a normal try-catch block in a traditional .NET application. In fact, they are a normal .NET exception. The major challenge for a service is to propagate the exception information to the user. The reason for this is that the user of the service will most probably be consuming the service in the from of a document and SOAP message format and secondly because the consumer of the service could be in some other technology and sending him a .NET specific exception doesn't make any sense.
In a service based application, when the applications are talking in terms of documents, in other words soap messages then the error and exception reporting from the service to the service consumer also is in the form of a SOAP element.
A WCF service provides a very nice abstraction over those in the form of a FaultException and the service is able to catch the exception using a FaiultException class. Even if we try with a normal Exception class in WCF, it will not work.
Try with normal Exception class

In this example we will try to implement a normal exception class within the WCF application. In this example we are trying to divide a number by itself. When we pass 0 as a parameter it will through an exception.

    public class Service1 : IService1

    {

        public string Devide(int value)

        {

            try

            {

               value  = value / value;

               return Convert.ToString(value);

            }

            catch (Exception ex)

            {

                throw new Exception("Value should be greater then 0");

            }

        }

    }

Here is sample output. We are seeing that a normal Exception class is unable to throw a new exception to the client.

WCF1

The solution

The solution is to throw an object of the FaultException class. In the following example we will implement one full working exception example using the FaultException class. Try to use the following procedure.

Step1: Create WCF application

Create a WCF application and add the following code to it.

Code for Service contract.

This is small and easy code to implement a service contract. Don't forget to put a "[FaultContraact]" attribute over the Devide function. The FaultInfo function will be defined in the next step.
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Web;

using System.Text;

 

namespace WCF

{

    [DataContract()]

    public class FaultInfo

    {

        [DataMember()]

        public string Reason = null;

    }

    public class Service1 : IService1

    {

        public string Devide(int value)

        {

            try

            {

               value  = value / value;

               return Convert.ToString(value);

              

            }

            catch (Exception ex)

            {

                FaultInfo fi = new FaultInfo();

                fi.Reason = "Value should not be in 0";

                throw new FaultException<FaultInfo>(fi, new FaultReason(fi.Reason));

            }

        }

    }

}

Within the catch we are creating an object of the FauException object and throwing it to the client.

Step 2: Create client application

This is a small console application that will consume the WCF service. We are sending 0 when we are trying to call the "Devide()" function and obviously it will throw an exception.
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;

using System.Collections;

using Client;

using Client.ServiceReference1;

using System.ServiceModel;

namespace Client

{

    class Program

    {

        static void Main(string[] args)

        {

            Client.ServiceReference1.Service1Client obj = new Service1Client();

            try

            {

                string value = obj.Devide(0);

                Console.WriteLine(value);

            }

            catch (FaultException ee)

            {

                Console.WriteLine(ee.Message);

            }

            Console.ReadLine();

        }

    }

}



We are seeing that the client code is able to catch the exception. Here is sample output.

WCF2

Conclusion

In this example we saw how to work with an exception in a WCF application. Hope you have understood the concepts. In the next article we will look more into WCF services. Happy learning

Up Next
    Ebook Download
    View all
    Learn
    View all