Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Exception Handling in WCF using Fault Contract

In this article, you will learn how to implement Exception Handling in WCF using fault contract. What is Fault Exception in WCF and why do we use it? Previously I have explained Create WCF REST Service in C#, How to Consume WCF REST Service in C#.

Fault Exception in WCF

The fault exception is used to pass the custom error message from the server to the client, inside the SOAP response XML. When the response is received by the client, the custom exception message is set by the service and is also available in the tag in the SOAP.

In order to do this, the service needs to throw a new instance of the FaultException class, with the custom message set in its constructor. Let’s say, we pass the custom message “Service was unable to divide by zero” in the constructor.

Exception Handling in WCF using Fault Contract

The following example will demonstrate how WCF service exception handling works using [FaultContract]. Find the below code snippet:-

First, you need to create a WCF service application to DivideNumber as shown below:

IService1.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfServiceFault
{    
   [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [FaultContract(typeof(HandleException))]
        int GetData(int value, int value2);
    }

    [DataContract]
    public class HandleException
    {
        [DataMember]
        public string CustomExceptionMessage { get; set; }

        [DataMember]
        public string ErrorDesc { get; set; }
    }
}

Service1.svc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfServiceFault
{
    public class Service1 : IService1
    {
        public int GetData(int value, int value2)
        {
            HandleException Obj = new HandleException();
            try
            {
                return (value / value2);
            }
            catch (Exception ex)
            {
                Obj.CustomExceptionMessage = "An Application Error has occurred";
                Obj.ErrorDesc = ex.ToString();
                throw new FaultException(Obj, ex.ToString());
            }
        }
    }
}

Now, add a new project -> ASP.Net Web Application (Client app) to the solution to call WCF web service and test the implementation.

Default.aspx






    

C# - WCF Exception Handling Implementation By Fault Contract

Enter Value1:
Enter Value2:

Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ClientWeb
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
        }


        protected void btnEnter_Click(object sender, EventArgs e)
        {
            try
            {
                ServiceReference1.Service1Client proxyClient = new ServiceReference1.Service1Client();
                int val = proxyClient.GetData(int.Parse(txtInput1.Text), int.Parse(txtInput2.Text));

                Response.Write("");
            }
            catch (FaultException ex)
            {
                lblErrorMessage.Visible = true;
                lblErrorMessage.Text = ex.Detail.CustomExceptionMessage + "
" + ex.Detail.ErrorDesc; lblErrorMessage.ForeColor = System.Drawing.Color.Red; } } } }

Download Source Code

Conclusion

I hope you liked this article on exception handling in WCF using FaultContract. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

The post Exception Handling in WCF using Fault Contract appeared first on DotNetTec.



This post first appeared on Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps API Developer, please read the originial post: here

Share the post

Exception Handling in WCF using Fault Contract

×

Subscribe to Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps Api Developer

Get updates delivered right to your inbox!

Thank you for your subscription

×