google-site-verification=ECX1M6_Vb39ty4VtQDXQce6wOjmPTxSettd3hTIZb9Q

C# - WCF Exception Handling Implementation By Fault Contract

random

C# - WCF Exception Handling Implementation By Fault Contract

Exception Handling in WCF


In the previous article I explained About WCF - An Introduction with Example, Creating WCF REST Services in ASP.NET Web Based Applications, Consume Restful WCF service in ASP.NET Web Application Examples

In this article I will explain about WCF Exception Handling Implementation using Fault Contract. We will look into managing service-oriented WCF error-handling paradigm, namely faults. The underlying principle of service-oriented error handling consists of SOAP fault messages, which convey the failure semantics and additional information associated with the failure (such as the reason).


Fault Handling in WCF Services


Most services which require error handling also require additional information to be passed with the error notification. This information can be transferred to the client as a standard WCF data contract, in the disguise of a fault. The contractual specification that a particular service operation can result in the specified fault is called a fault contract. Find the source code below:- 

exception handling,error handling c#,Exception Handling in WCF
IService1 (Interface)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Exception_Handling_WCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [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 (Class)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Exception_Handling_WCF
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    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<HandleException>(Obj, ex.ToString());
            }
        }
    }
}
Client WebApp Demo
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>C# - WCF Exception Handling Implementation By Fault Contract</title>
</head>
<body style="font-family: Arial;">
    <form id="form1" runat="server">
    <h4>
        C# - WCF Exception Handling Implementation By Fault Contract</h4>
    <div>
        <table>
            <tr>
                <td>
                    Enter Value1:
                </td>
                <td>
                    <asp:TextBox ID="txtInput1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Enter Value2:
                </td>
                <td>
                    <asp:TextBox ID="txtInput2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                    <asp:Button ID="btnEnter" runat="server" Text="Result" OnClick="btnEnter_Click" />
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Label ID="lblErrorMessage" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblErrorMessage.Text = "";
    }
    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("<script language='javascript'>alert('Result: " + val + "');</script>");
        }
        catch (FaultException<ServiceReference1.HandleException> ex)
        {
            lblErrorMessage.Visible = true;
            lblErrorMessage.Text = ex.Detail.CustomExceptionMessage + "<br/>" + ex.Detail.ErrorDesc;
        }
    }
}
Download the source code, click here
Please leave your comments, suggestions and queries about this post in the comment sections in order for me to improve my writing skills and to showcase more useful posts. Thanks for reading .. :)
C# - WCF Exception Handling Implementation By Fault Contract Reviewed by Ravi Kumar on 2:44 PM Rating: 5

No comments:

All Rights Reserved by Etechpulse © 2012 - 2017

Contact Form

Name

Email *

Message *

Powered by Blogger.