Exception handling in WCF – Part 9

In this article we learn what is exception handling and how we can implement in wcf.

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

Exceptions are nothing but unseen error occurs when executing code logic.

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

Step 1: Delete the default service DoWork and Add new service. My IMathService.cs file look like

using System.ServiceModel;namespace wcfService
{
[ServiceContract]
public interface IMathService
{
[OperationContract]
int AddTwoNo(int FirstNo, int Second);
}

[OperationContract]
int DivideTwoNo(int first, int second);

}

Step 2: Lets make changes in MathService.svc.cs. Lets implement IMathService interface.

using System;
namespace wcfService
{
public class MathService : IMathService
{
public int AddTwoNo(int FirstNo, int Second)
{
return FirstNo + Second;
}

public int DivideTwoNo(int first, int second)
{
return first / second;
}
}
}

As we know, default binding of wcf services is httpbasicbinding. Let’s run the application and test that it works fine or not.

devide-output-success

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

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

In AddNumber.cs

namespace mvcClient.Models
{
public class AddNumber
{
public int FirstNo { get; set; }
public int SecondNo { get; set; }
}

public class DivideNumber
{
public int FirstNo { get; set; }
public int SecondNo { get; set; }
}
}

In HomeController.cs

using System.Web.Mvc;
namespace mvcClient.Controllers
{
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Models.AddNumber addNumber) {
// MathServiceReference is reference object name which is we are provide when i am trying to add web reference in my project

MathServiceReference.MathServiceClient client = new MathServiceReference.MathServiceClient();
int Result = 0;
Result = client.AddTwoNo(addNumber.FirstNo, addNumber.SecondNo);
ViewBag.Result = Result;
return View();
}
}
}

I am trying to add divide ActionResult on home controller.

trying-to-call-divide-funtion

I am not getting divide method from wcf service becouse of I am not updated service reference in my project.

update-service-reference

I have added this following code in homecontroller.

[HttpPost]
public ActionResult DevideTwoNo(Models.DivideNumber divideNumber)
{
MathServiceReference.MathServiceClient client = new MathServiceReference.MathServiceClient();
int Result = 0;
Result = client.DivideTwoNo(divideNumber.FirstNo, divideNumber.SecondNo);
ViewBag.DivideResult = Result;
return View();
}

In Index.cshtml

@model mvcClient.Models.DivideNumber
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>
@using (Html.BeginForm("DevideTwoNo", "Home", FormMethod.Post))
{
<div>
<span>First No :</span> @Html.TextBoxFor(m=>m.FirstNo)
</div>
<div style="clear:both; height:1px; width:1px;"></div>
<div>
<span>Second No :</span> @Html.TextBoxFor(m=>m.SecondNo)
</div>
<div style="clear:both; height:1px; width:1px;"></div>
<div>
<input type="submit" value="Divide Number" /><br />
<span>Result : @ViewBag.DivideResult </span>
</div>
}

After make these changes i am going to run this application and see the output.

output

Service is running successfully.

Now we are going to pass 0 as denominator and see the output.

error

When we devide by zero it’s thrown an exception and that exception is .net exception. In WCF we cannot send back .net exception to client directly. That exception need be serializing in xml format before sending to client. That xml is called soap fault. By default wcf application not include an unhandled exception details in soap fault exception.

I am not able to find what the error is thrown by wcf application. For debugging purpose I am going to write some code in web.cofig file locate in wcfService project.

<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>

And that behavior configuration going to call in service.

<service name="wcfService.MathService" behaviorConfiguration="mexBehaviour">

Now going to run the application and see the output.

devide-by-zero-error

Now I am getting a meaningful error. Now I know that this is divide by zero exception. Now I am able to fix this issue. Lets 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.trace-viewer-result

Now i am going to make some changes in code.

HomeController.cs

using System.ServiceModel;

[HttpPost]
public ActionResult DevideTwoNo(Models.DivideNumber divideNumber)
{
try
{
MathServiceReference.MathServiceClient client = new MathServiceReference.MathServiceClient();
int Result = 0;
Result = client.DivideTwoNo(divideNumber.FirstNo, divideNumber.SecondNo);
ViewBag.DivideResult = Result;
}
catch (FaultException FE)
{
ViewBag.DivideResult = FE.Message;
}
return View("Index");
}

Let’s run the application and see the output.

output-enable-fault

I hope you will enjoy this article.

Happy coding.

Tags: , , ,