Thursday, 4 October 2012

What is Fault Contract?


what is FaultContract?

Fault Contract is used to expose an exception to client in friendly manner. why because exceptions are not belogs a  technology specific. so .net exceptions are technology specific(.net) these exceptions are not aware of other platforms. so to expose these .net  exceptions to clients of other technologies.(soa specifics: exceptions should be universal understand format). so wcf provides Faultcontract to expose about exceptions in wcf.

In WSDL we have a tag is <FaultTo> xml element exposes this exceptions to clients.

Fault Contract Demo

namespace FaultServiceDemo
{
     public class Service1 : IService1
    {
        SqlConnection con = new SqlConnection("uid=sa;pwd=zolt123$;database=wcfdb");

        SqlDataAdapter da;
        DataSet ds;
        #region IService1 Members

        public Employee Getdata(int EmpId)
        {
          
            da = new SqlDataAdapter("select * from emp where eid=" + EmpId, con);
            ds = new DataSet();
            da.Fill(ds);

            if (ds.Tables[0].Rows.Count == 0)
            {
                //create an object for the custom SOAP Fault.
                //and initialize the falut with our own customized fault message.

 CustomFaultMsg custmErr = new CustomFaultMsg { MyCustomErrMsg = "There is no employee exist with the emp id : " + EmpId };


                //Raise the customized fault message exception to the client.
                throw new FaultException<CustomFaultMsg>(custmErr);

            }

          // if the Emp record is exist , we have to return the emp Details....

            else
            {
                // Create an employee object and store all the emp record details in to the
                // employee object.

                Employee eobj = new Employee();
                eobj.Ename = ds.Tables[0].Rows[0][1].ToString();
                eobj.Eloc = ds.Tables[0].Rows[0][2].ToString();
                eobj.Deptid = Convert.ToInt32(ds.Tables[0].Rows[0][3].ToString());
                //   eobj.Uploadstatus = Convert.ToBoolean(ds.Tables[0].Rows[0][4].ToString());
                return eobj;
            }
        }
    }
}




At Client





            using (Service1Client proxy = new Service1Client())
            {
            
                string empid = Console.ReadLine();
                int eid = Convert.ToInt32(empid);
                try
                {
                    Employee emp = proxy.Getdata(eid);
                    Console.Write(emp.Ename);

                }
                catch (FaultException<CustomFaultMsg> faultMsg)
                {
                    // CustomFaultMsg obj = new CustomFaultMsg();
                    string error = faultMsg.Detail.MyCustomErrMsg;
                    Console.WriteLine(error);
                    Console.WriteLine(proxy.State);
                    //proxy.Close();

                    proxy.Abort();
                    Console.WriteLine(proxy.State);

                }
                catch (FaultException<CommunicationException> cex)
                {

                    string str=cex.Detail.Message;
                    Console.WriteLine(str);
               
                }                             

            }
            Console.Read();

No comments:

Post a Comment