Monday, July 22, 2013

What is a Fault Contract in WCF Service?

What is Fault Contract?
Any unexpected error occurred in WCF service can be returned to the client by using Fault Contract.

Process to use Fault Contract in WCF service.

WCF Service:
Step 1: 
Add a data contract in WCF service to return error details to the client; you can also use your existing data contract by adding more properties in it to return error details.

Ex:-
   [DataContract]
    public class MYWCFFaultContract
    {
        [DataMember]
        public bool Result { get; set; }
        [DataMember]
        public string ErrorMessage { get; set; }
        [DataMember]
        public string ErrorDetails { get; set; }
    }

Step 2: 
Add fault contract attribute in the interface Method.

Ex:-
      [ServiceContract]
    public interface IMyWCFService
    {
       [OperationContract]
       [FaultContract(typeof(MYWCFFaultContract))]
       void InsertEmployee(MyWCFDataContract obj);
    }

Step 3: 
Add try catch block in your method, set value to the properties of fault under catch and throw the exception out of the service
Try
{
  \\ Your code
}
catch (Exception ex)
             {
                 objResult.Result = false;
                 objResult.ErrorMessage = "Error Occured in InsertEmployee() method";
                 objResult.ErrorDetails = ex.ToString();
                 throw new FaultException<MYWCFFaultContract>(objResult, ex.ToString());
             }


Client Application:
Step 1: 
Add reference to below namespace on page
      using System.ServiceModel;

Step 2: 
Add try catch block in your code and under catch write the code handle the exception thrown by service
Try
   {
     \\ Your code
   }
catch (FaultException<ServiceReference1.MYWCFFaultContract> excep)
   {
     lblException.Text = excep.Detail.ErrorMessage + " " + excep.Detail.ErrorDetails;
   }


So by this way details of error occurred in service can be returned to the client.


Sunday, July 14, 2013

Transactions in WCF Service

A. Changes in WCF service to support Transactions

Step 1:
Create a method in Interface and add transaction attribute “[TransactionFlow(TransactionFlowOption.Allowed)]” on it.

[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
void InsertEmployee(employee emp);

Step 2:
In service class add operation behaviour on method to enable transaction

[OperationBehavior(TransactionScopeRequired=true)]
public void InsertEmployee(employee emp)
{
}

Step 3:
In Service app config, add wsHHTPBinding end point
<endpoint address ="http://localhost:8731/Design_Time_Addresses/WcfServiceLibrary1/Service1/" binding="wsHttpBinding" bindingConfiguration="TransactionalBind" contract="WcfServiceLibrary1.IService1">

Step 4:
Create binding configuration and add its name in endpoints under “binding configuration” property
<bindings>
      <wsHttpBinding>
        <binding name="TransactionalBind" transactionFlow="true"></binding>
      </wsHttpBinding>
</bindings>


B. Changes while consuming WCF service in Web Application:

Step 1:
Consume the WCF service by adding reference

Step 2:
Add transaction scope in method

        using (TransactionScope a = new TransactionScope(TransactionScopeOption.RequiresNew))
        {
            try
            {
                obj.InsertEmployee(new ServiceReference1.employee());
                //throw new Exception();
                a.Complete();
            }
            catch
            {
                a.Dispose();
            }
        }
       Transaction will complete when complete method will call in transaction scope.In case of any exception it’ll rollback the transaction done.