.NET provides a very simple way of dealing with exceptions using try-catch-throw to provide a user an understandable message for a system generated exception.
But a WCF service is consumed by some other application, not an end user. If something goes wrong in a service, the developer of the client application should know what's going wrong in the service. Again, the client application developer shouldn't know each and every exception in detail, it may lead to some security issues. Also, sometimes a client should be instructed using exceptions.
Again, the Exception class is .NET specific and a WCF Service is consumed by an application that may be in some other language like Java, PHP, ROR and so on. So exceptions may or may not be understood by the client applications.
So, things should be under the control of the service end. For this we have FaultException and FaultContract. Now, let's dive into exception handling in WCF.
Let's create a service that will perform a division operation. We will use the “DivideByZeroException” to demonstrate Exception handling in a WCF service.
I believe you know how to create a WCF service and consume it. You can download the democode for a better understanding on “Exception handling in WCF service”.
IArithmaticService.cs
- using System.Runtime.Serialization;
- using System.ServiceModel;
-
- namespace FaultContractDemo
- {
- [ServiceContract]
- public interface IArithmaticService
- {
- [OperationContract]
- int Divide(int Divident, int Divisor);
- }
- }
ArithmaticService.svc.cs
- using System;
- using System.ServiceModel;
-
- namespace FaultContractDemo
- {
- public class ArithmaticService : IArithmaticService
- {
- public int Divide(int Divident, int Divisor)
- {
- int Quotient;
- try
- {
- Quotient = Divident / Divisor;
- }
- catch (Exception Ex)
- {
- throw Ex;
- }
- return Quotient;
- }
- }
- }
Now, let's create a client application to consume this service.
Form1.cs
- using System;
- using System.ServiceModel;
- using System.Windows.Forms;
-
- namespace ClientApp
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void butDivide_Click(object Sender, EventArgs E)
- {
- try
- {
- int Divident, Divisor;
- int.TryParse(txtFirstNum.Text, out Divident);
- int.TryParse(txtSecondNum.Text, out Divisor);
- var ObjProxy = new ArithmaticServiceReference.ArithmaticServiceClient();
- lblQuotient.Text = ObjProxy.Divide(Divident, Divisor).ToString();
- }
- catch (Exception Ex)
- {
- lblException.Text = Ex.Message;
- }
-
- }
- }
- }
2 non-zero numbers, you should get their quotient.
Now, zero for the divisor. You will get an exception as follows.
This exception message indicates something went wrong in the service. But, this is not giving any clear message of what went wrong.
Since the service and client applications are interacting with each other using SOAP, we need to exception details in SOAP format. We can use FaultException &/or FaultContract (in System.ServiceModel namespace) to these exception details to the client.
FaultExceptionLet's see how to use a FaultException.
- You need to create an instance of the FaultException class with FaultReason/FaultCode/MessageFault and throw it.
- In the client application add a catch block with an instance of FaultException and use it's properties.
ArithmaticService.svc.cs
- using System;
- using System.ServiceModel;
-
- namespace FaultContractDemo
- {
- public class ArithmaticService : IArithmaticService
- {
- public int Divide(int Divident, int Divisor)
- {
- int Quotient;
- try
- {
- Quotient = Divident / Divisor;
- }
- catch (Exception Ex)
- {
- throw new FaultException(Ex.ToString());
- }
- return Quotient;
- }
- }
- }
Form1.cs
- using System;
- using System.ServiceModel;
- using System.Windows.Forms;
-
- namespace ClientApp
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void butDivide_Click(object Sender, EventArgs E)
- {
- try
- {
- int Divident, Divisor;
- int.TryParse(txtFirstNum.Text, out Divident);
- int.TryParse(txtSecondNum.Text, out Divisor);
- var ObjProxy = new ArithmaticServiceReference.ArithmaticServiceClient();
- lblQuotient.Text = ObjProxy.Divide(Divident, Divisor).ToString();
- }
- catch (FaultException Ex)
- {
- lblException.Text = Ex.Message;
- }
- }
- }
- }
Now, if you zero for the divisor then you will get an exception like:
FaultContract
If you want to send some additional details (maybe to describe some business requirements) to the client then you can use a FaultContract.
- Create a custom class with properties for holding the details you want to send to the client application.
- Decorate this custom class with a DataContract attribute and it's properties with a DataMember attribute.
- Decorate the method with a FaultContract attribute with the type of object you want to throw.
- In the client application, create a catch block with an instance of the custom fault class and use it's properties.
Add a class to the IArithmaticService interface in IArithmaticService.cs as in the following:
- using System.Runtime.Serialization;
- using System.ServiceModel;
-
- namespace FaultContractDemo
- {
- [ServiceContract]
- public interface IArithmaticService
- {
- [OperationContract]
- [FaultContract(typeof(CustomExceptionDetails))]
- int Divide(int Divident, int Divisor);
- }
-
-
-
-
- [DataContract]
- public class CustomExceptionDetails
- {
- [DataMember]
- public int Id { get; set; }
-
- [DataMember]
- public string Message { get; set; }
-
- [DataMember]
- public string Details { get; set; }
- }
- }
ArithmaticService.svc.cs
- using System;
- using System.ServiceModel;
-
- namespace FaultContractDemo
- {
- public class ArithmaticService : IArithmaticService
- {
- public int Divide(int Divident, int Divisor)
- {
- int Quotient;
- try
- {
- if (Divisor == 0)
- {
- var ObjCustom = new CustomExceptionDetails
- {
- Message = "Divisor should be non-zero.",
- Details = @"Dividing a number by 0 will always give you infinity, so enter a non-zero number for divisor.",
- Id = 1
- };
- throw new FaultException<CustomExceptionDetails>(ObjCustom);
- }
- Quotient = Divident / Divisor;
- }
- catch (Exception Ex)
- {
- throw Ex;
- }
- return Quotient;
- }
- }
- }
Form1.cs
- using System;
- using System.ServiceModel;
- using System.Windows.Forms;
-
- namespace ClientApp
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void butDivide_Click(object Sender, EventArgs E)
- {
- try
- {
- int Divident, Divisor;
- int.TryParse(txtFirstNum.Text, out Divident);
- int.TryParse(txtSecondNum.Text, out Divisor);
- var ObjProxy = new ArithmaticServiceReference.ArithmaticServiceClient();
- lblQuotient.Text = ObjProxy.Divide(Divident, Divisor).ToString();
- }
- catch (FaultException<ArithmaticServiceReference.CustomExceptionDetails> Ex)
- {
- lblException.Text = "Id: " + Ex.Detail.Id + "\nMessage: " + Ex.Detail.Message +
- "\nDetails: " + Ex.Detail.Details;
- }
- catch (FaultException Ex)
- {
- lblException.Text = Ex.Message;
- }
- catch (Exception Ex)
- {
- lblException.Text = Ex.Message;
- }
- }
- }