Microsoft > WCF >> Code Snippets Views : 5824
WCF Fault Contract
Rate This Article :

Fault Contract in WCF

This article explains how to define and use the FaultContract in the WCF Service.

Use of Fault Contract:

As we know, Client and Service Communicates through SOAP Messages.  If there are any exception occurred in the Service, Service layer will not be able to send exceptions to the client as a service exception.

By using Fault Contract, Service will be able to send exceptions through SOAP Fault Contracts so that the Client will be able to detect the errors occurred in the service.

I have explained in simple easy steps below,

Service Configuration

Step 1:

Create a Contract “FaultContractModal” that will hold the execption’s message and its description occurred in the business logics.

    [DataContract]

    public class FaultContractModal

    {

        [DataMember]

        public string Message { get; set; }

        [DataMember]

        public string Description { get; set; }

    }

 Step 2:

Decorate the Service “OperationContract” with “FaultContract” as mentioned below.

    [ServiceContract]

    public interface IService1

    {

        [OperationContract]

        // Below attribute to define the fault contract to send the exception details to client.

        [FaultContract(typeof(FaultContractModal))]

        string GetData(int value);

    }

Step 3:

Do your business logic and assign the exception to the Fault Contract as mentioned below.

   public class Service1 : IService1

    {

        public string GetData(int value)

        {

            if (value == 99)

            {

                var modal = new FaultContractModal();

                modal.Message = "This is Fault Message";

                modal.Description = "Value 99 is not valid.";

 

                throw new FaultException<FaultContractModal>(modal);

            }

 

            return string.Format("You entered: {0}", value);

        }

    }

Client Configuration

Step 4:

Add/update the service reference in the client where the service will be consumed. Write the Catch block to accept the FaultException as mentioned below.

static void Main(string[] args)

        {

            try

            {

                using (MyServiceReference.Service1Client client = new MyServiceReference.Service1Client())

                {

                    var readLineKey = Console.ReadLine();

                    var data = client.GetData(Convert.ToInt32(readLineKey));

 

                    Console.WriteLine(data);

                }

            }

            catch (FaultException<FaultContractModal> fex)

            {

                Console.WriteLine(fex.Detail.Message);

                Console.WriteLine(fex.Detail.Description);

            }

            catch (FaultException ex)

            {

                Console.WriteLine(ex.Message);

            }

 

            Console.Read();

 

        }

Test It:

1. Pass “99” as an input from client. Fault exception will be thrown from the service.

FaultContract_Exception.png
About Author
Raj Kumar
Total Posts 57
Developer in .Net!
Comment this article
Name*
Email Address* (Will not be shown on this website.)
Comments*
Enter Image Text*
   
View All Comments
Comments not posted yet!! Please post your comments on this article.
  Privacy   Terms Of Use   Contact Us
© 2016 Developerin.Net. All rights reserved.
Trademarks and Article Images mentioned in this site may belongs to Microsoft and other respective trademark owners.
Articles, Tutorials and all other content offered here is for educational purpose only and its author copyrights.