Exception Handling in WCF: Part 9

Before reading this article, I highly recommend reading my previous parts:

Exceptions are nothing but unseen errors that occur when executing code logic.

I am using my previous project for this demo and adding some method to it.

Step 1

Delete the default service DoWork and add a new service. My IMathService.cs file looks like.

  1. using System.ServiceModel;  
  2. namespace wcfService  
  3. {  
  4.     [ServiceContract]  
  5.     public interface IMathService  
  6.     {  
  7.         [OperationContract]  
  8.         int AddTwoNo(int FirstNo, int Second);     
  9.     }  
  10. [OperationContract]  
  11.         int DivideTwoNo(int first, int second);  
  12. }  

Step 2

Let's make changes in MathService.svc.cs. Let's implement an IMathService interface.

  1. using System;  
  2. namespace wcfService  
  3. {  
  4.     public class MathService : IMathService  
  5.     {  
  6.         public int AddTwoNo(int FirstNo, int Second)  
  7.         {  
  8.             return FirstNo + Second;  
  9.         }  
  10. public int DivideTwoNo(int first, int second)  
  11.         {  
  12.             return first / second;  
  13.         }  
  14.     }  
  15. }  
As we know, the default binding of WCF services is httpbasicbinding. Let's run the application and ensure it works fine.

httpbasicbinding

Assign the value of FirstNo, Second 6 and 2 respectively and hit the invoke button and see the output, it should be 3. This means my service is working fine.

If you are new to how to call a WCF service in a web project then I must recommend to please read my previous article Introduction to WCF- Part 1.

In AddNumber.cs
  1. namespace mvcClient.Models  
  2. {  
  3.     public class AddNumber  
  4.     {  
  5.         public int FirstNo { getset; }  
  6.         public int SecondNo { getset; }  
  7.     }  
  8. public class DivideNumber  
  9.     {  
  10.         public int FirstNo { getset; }  
  11.         public int SecondNo { getset; }  
  12.     }  
  13. }  
In HomeController.cs
  1. using System.Web.Mvc;  
  2. namespace mvcClient.Controllers  
  3. {  
  4.     public class HomeController : Controller  
  5.     {  
  6.         [HttpGet]  
  7.         public ActionResult Index()  
  8.         {  
  9.             return View();  
  10.         }  
  11.   
  12.         [HttpPost]  
  13.         public ActionResult Index(Models.AddNumber addNumber) {  
  14.         // MathServiceReference is reference object name which is we are provide when i am trying to add web reference in my project   
  15. MathServiceReference.MathServiceClient client = new MathServiceReference.MathServiceClient();  
  16.             int Result = 0;  
  17.             Result = client.AddTwoNo(addNumber.FirstNo, addNumber.SecondNo);  
  18.             ViewBag.Result = Result;  
  19.               
  20.             return View();  
  21.         }  
  22.     }  
  23. }  
I am trying to add and divide ActionResult on the home controller.

ActionResult

I am not getting a divide method from the WCF service because I have not updated the service reference in my project.

WCF service

I have added the following code in homecontroller.
  1. [HttpPost]  
  2.        public ActionResult DevideTwoNo(Models.DivideNumber divideNumber)  
  3.        {  
  4.            MathServiceReference.MathServiceClient client = new MathServiceReference.MathServiceClient();  
  5.            int Result = 0;  
  6.            Result = client.DivideTwoNo(divideNumber.FirstNo, divideNumber.SecondNo);  
  7.   
  8.            ViewBag.DivideResult = Result;  
  9.            return View();  
  10.        }  
In Index.cshtml
  1. @model mvcClient.Models.DivideNumber  
  2. @{  
  3.     ViewBag.Title = "Index";  
  4.     Layout = "~/Views/Shared/_Layout.cshtml";  
  5. }  
  6.   
  7. <h2>Index</h2>  
  8. @using (Html.BeginForm("DevideTwoNo""Home", FormMethod.Post))  
  9. {  
  10.    <div>  
  11.    <span>First No :</span> @Html.TextBoxFor(m=>m.FirstNo)  
  12.        </div>  
  13.        <div style="clear:both; height:1px; width:1px;"></div>  
  14.    <div>  
  15.    <span>Second No :</span> @Html.TextBoxFor(m=>m.SecondNo)  
  16.        </div>  
  17.        <div style="clear:both; height:1px; width:1px;"></div>  
  18.        <div>  
  19.    <input type="submit" value="Divide Number" /><br />  
  20.            <span>Result : @ViewBag.DivideResult </span>  
  21.        </div>  
  22. }  
After making these changes I will run this application and see the output.

run this application

The service is running successfully.

Now we will pass 0 as the denominator and see the output.

see the output

When we divide by zero it throws an exception and that exception is a .Net exception. In WCF we cannot return a .Net exception to the client directly. That exception must be serialized in XML format before it can be sent to the client. That XML is called a SOAP fault. By default WCF applications do not include unhandled exception details in SOAP fault exceptions.

I am not able to find what the error is that is thrown by a WCF application. For debugging purposes I will write some code in the web.cofig file locates in the wcfService project.
  1. <behaviors>  
  2.   <serviceBehaviors>  
  3.     <behavior name="mexBehaviour">  
  4.       <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>  
  5.       <serviceDebug includeExceptionDetailInFaults="true"/>  
  6.     </behavior>  
  7.   </serviceBehaviors>  
  8. </behaviors>  
And that behavior configuration is called the service.
  1. <service name="wcfService.MathService" behaviorConfiguration="mexBehaviour">  
Now to run the application and see the output.

run the application

Now I am getting a meaningful error. Now I know that this is a divide by zero exception. I will now fix this issue. Let's enable message logging and see in trace viewer. If you are new to Tracing and message logging the please read my previous blog on it.

getting a meaningful error

Now I will make some changes in the code.

HomeController.cs
  1. using System.ServiceModel;  
  2. [HttpPost]  
  3.  public ActionResult DevideTwoNo(Models.DivideNumber divideNumber)  
  4.  {  
  5.      try  
  6.      {  
  7.          MathServiceReference.MathServiceClient client = new MathServiceReference.MathServiceClient();  
  8.          int Result = 0;  
  9.          Result = client.DivideTwoNo(divideNumber.FirstNo, divideNumber.SecondNo);  
  10.   
  11.          ViewBag.DivideResult = Result;  
  12.      }  
  13.      catch (FaultException FE)  
  14.      {  
  15.          ViewBag.DivideResult = FE.Message;  
  16.      }  
  17.      return View("Index");  
  18.  }  
Let's run the application and see the output.

output

I hope you will enjoy this article.

Happy coding.

Up Next
    Ebook Download
    View all
    Learn
    View all