Monday, January 31, 2011

wcf excepton handling

Exception handling
------------------

step 1:
-------
under web.config(webhost) set to true

 <serviceDebug includeExceptionDetailInFaults="true"/>

step:2
-------
under service.cs

add under namespace(1st stmt)
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]

under code
==========

try
{
  con.open()

}
catch(Exception ex)
{
  throw new FaultException("can't connect to the database");
}


under clint code
------------------

try
{
   customers = proxcy.ListofCustomer();
   customerBindingSource.Datasource = customers;
}
cathch (FaultException faultException) // fault exception occurs i will show this msg.
{
MessageBox.Show(faultException.Message);
}

cathch (FaultException faultException) //any other exception occurs i will show this msg.
{
MessageBox.Show(ex.Message);
}

------------------------------------------------------------------------------------------------------
type2
------------------------------------------------------------------------------------------------------

step 1:
-------
under web.config(webhost) set to true

 <serviceDebug includeExceptionDetailInFaults="true"/>

step:2
-------
under service.cs
----------------

add under namespace(1st stmt)
[ServiceBehavior(IncludeExceptionDetailInFaults=true)]

under code
==========

try
{
  con.open()

}
catch(Exception ex)
{
  throw new FaultException("can't connect to the database",new FaultCode("DBConnection");
}

try
{
  data reader code.......
  while(dr.read())
  {
     obj.value = dr.getString(200); // if problem occuurs here
  }
}
catch(Exception ex)
{
    throw new FaultException("Error reading from the database",new FaultCode(DBReader));
}


under clint code
------------------

try
{
   customers = proxcy.ListofCustomer();
   customerBindingSource.Datasource = customers;
}
cathch (FaultException faultException) // fault exception occurs i will show this msg.
{
    swithc (faultException.code.Name)
    {
       case "DBConnection":
        MessageBox.Show(faultException.Message + "\n\n" + "try again later.", "connection problem");
        break;
        case "DBReader":
         MessageBox.Show(faultException.Message + "\n\n" + "contact the administrator.", "Reading

problem");
        break;
        default:
           MessageBox.Show(faultException.Message + "\n\n" + "contact the administrator.", "unknown

problem");
         break
    }
       
      

}

cathch (FaultException faultException) //any other exception occurs i will show this msg.
{
MessageBox.Show(ex.Message);
}

======================================================================================================

========================================================================

type3:

localiging exception message:
-----------------------------

======================================================================================================

=========================================================================

  strongly typed SOAP FAULTS ( exception handling (client may java or other platform to receive

exceptions))
  -------------------------------------------------------------------------------------------

  1) CREATE ONE OR MORE FAULT CLASSES AND ADD THEM TO THE DATACONTRACT.



   UNDER ISERVICE.CS
   --------------------


   [ServiceContract]
   public interfase Iservice1
   {
      [OperationContract]
      [FaultContract(typeof(ConnectionFault))]
      [FaultContract(typeof(DataFault))]
      string hello();


      [OperationContract]
      [FaultContract(typeof(ConnectionFault))]
      [FaultContract(typeof(DataFault))]
      string hai(string hi);

   }






   UNDER [DATACONTRACT]  ADD TWO NCL
   ====================

   public class ConnectionFault
   {
     [DataMember]
     public string Operation;

     [DataMember]
     public string Reason;


    [DataMember]
     public string Details;
   
   }

   public class DataFault
   {
     [DataMember]
     public string Operation;

     [DataMember]
     public string Reason;


    [DataMember]
     public string Details;
   
   }



  under Service.cs
  -----------------
 

under code
==========

try
{
  con.open()

}
catch(Exception ex)
{
  var connectionFault  = new ConnectionFault();
  connectionFault.Operaion = "hello";
  connectionFault.Reason = "Can't connect to the database";
  connectionFault.Details = ex.Message;
  throw new FaultException<ConnectionFault>(connectionFault,new FaultReason("can't connect to the

database"));  
}

try
{
  data reader code.......
  while(dr.read())
  {
     obj.value = dr.getString(200); // if problem occuurs here
  }
}
catch(Exception ex)
{
   var dataFault  = new DataFault  ();
  dataFault.Operation  = "hello";
  dataFault.Reason = "Error reading from the database";
  dataFault.Details = ex.Message;
  throw new FaultException<DataFault>(dataFault,new FaultReason("Error reading from the database"));
}


under client
===========================


under clint code
------------------

try
{
   customers = proxcy.ListofCustomer();
   customerBindingSource.Datasource = customers;
}

catch(FaultException<ConnectionFault>connectionException)
{
   MessageBox.Show(String.Format("{0}\n\n" + "The following occured in {1}:\n{2}",
   connectionException.Detail.Reason,
   connectionException.Deatil.Operation,
   connectionException.Detail.Details),
   "connction problem");
}

catch(FaultException<DataFault>dataException)
{
   MessageBox.Show(String.Format("{0}\n\n" + "The following occured in {1}:\n{2}",
   connectionException.Detail.Reason,
   connectionException.Deatil.Operation,
   connectionException.Detail.Details),
   "reading problem");
}

No comments:

Post a Comment