In
my previous post i explained, Interview question and few details on WCF. Now in this article i will explain how WCF Transaction protocol.
Transaction
is an important factor in any business application which contain CRUD
operations. Here in WCF we have TransactionScope class which basically
manages the transaction and also detech the transaction scope.
for
ex. If your calling multiple methods and if any of method fails, then
entire transaction will be rolled back unless its outside boundary of
Scope.
WCF supports transaction on below bindings.
1. WSHttpBinding
2. WSFederationHttpBinding
3. NetNamedPipeBinding
4. NetTcpBinding
5. WSDualHttpBinding
You need to specify the TransactionFlow attribute on all the contracts where it required transaction to be handled. where
we have to specified that transaction are allowed for this specific
method by assigning enum as 'TransactionFlowOption.Allowed'
[ServiceContract]
public interface IUserDetails
{
[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
public void DeleteUserData();
[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
public void UpdateUserData();
}
there are following option present in TransactionFlow attribute.
TransactionFlowOption.Allowed :Transaction can be flowed.
TransactionFlowOption.NotAllowed: Transaction should not be flowed. This is default.
TransactionFlowOption.Mandatory: Transaction must be flowed.
Now implementing 'TransactionScopeRequired' attribute on specific method.
[OperationBehavior(TransactionScopeRequired = true)]
public void UpdateUserData()
{
// database call goes here.
}
Once the scope assing, the last process will be enabling the transaction flow in config file.
Now
consume the service by calling the UpdateUserData method and assign the
scope block which check if the transaction method fails, if so then it
will automatically rollback it.
using (TransactionScope Transcope = new TransactionScope())
{
try
{
Services.UserDetailsService.UserDetailsServiceClient()
clientVal = new Services.UserDetailsService.UserDetailsServiceClient();
// code goes here to call method and assing the value.
scope.Complete();
}
catch (Exception ex)
{
scope.Dispose();
}
}
I
know this will helps to understand the concepts of transaction in WCF ,
if you have any query or suggestion please put your comments to know
more.