This article is about how to implement WCF with Transaction at service level and self hosting in Windows application.
Transactions have four standard properties, called ACID properties:
Atomicity: ensures that all operations within the work unit are completed successfully; otherwise, the transaction is aborted at the point of failure, and previous operations are rolled back to their former state.
Consistency: ensures that the database properly changes states upon a successfully committed transaction.
Isolation: enables transactions to operate independently of and transparent to each other.
Durability: ensures that the result or effect of a committed transaction persists in case of a system failure.
Steps:
1. Add WCF project.
2. Add System.Transactions DLL to WCF Service project, this DLL contains Transaction related operations
3. Add your method to interface and decorate method with TransactionFlow attribute, 3 options are available in TransactionFlowOption enum
TransactionFlowOption.Allowed – transaction may or may not be followed
TransactionFlowOption.Mandatory – transaction must be followed
TransactionFlowOption.NotAllowed – transaction not allowed
Example:
4. Implement method with operation behaviour (set TransactionScopeRequire= True and TransactionAutoComplete=true) in interface as follows
By setting TransactionScopeRequire= True, we make the particular function can only be called with TransactionScope from client.
By setting TransactionAutoComplete=true, we make the transaction to commit automatically once its complete its process
Example:
[OperationBehavior(TransactionScopeRequired=true,TransactionAutoComplete=true)]
public void TransferAmountUsingSql(ulong amount)
{
using (SqlConnection conn=new SqlConnection("your Connection string here"))
{
using (SqlCommand cmd=new SqlCommand("your Cmd Text here",conn))
{
//your code logic here
cmd.ExecuteNonQuery();
}
}
}
5. Modify configuration file to allow transaction, by setting transactionFlow=”true”
<bindings>
<wsHttpBinding>
<binding name="transactionExample" transactionFlow="true"></binding>
</wsHttpBinding>
</bindings>
6. Create client application and consume WCF service as follows,
using (TransactionScope transScrope=new TransactionScope(TransactionScopeOption.Required))
{
try
{
ulong amount=Convert.ToUInt64(txtAmount.Value);
wcfTransaction.TransferAmountSuccess(amount);
lblResult.Text = "Succeed";
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
finally
{
transScrope.Complete();
}
}
}
Project Description:
I enclosed sample project to demonstrate Transaction in WCF with self hosting
Download WCF Sample
Transactions:
A transaction is a unit of work that is performed against a database. Transactions are units or sequences of work accomplished in a logical order, whether in a manual fashion by a user or automatically by some sort of a database program.Transactions have four standard properties, called ACID properties:
Atomicity: ensures that all operations within the work unit are completed successfully; otherwise, the transaction is aborted at the point of failure, and previous operations are rolled back to their former state.
Consistency: ensures that the database properly changes states upon a successfully committed transaction.
Isolation: enables transactions to operate independently of and transparent to each other.
Durability: ensures that the result or effect of a committed transaction persists in case of a system failure.
Steps:
1. Add WCF project.
2. Add System.Transactions DLL to WCF Service project, this DLL contains Transaction related operations
3. Add your method to interface and decorate method with TransactionFlow attribute, 3 options are available in TransactionFlowOption enum
TransactionFlowOption.Allowed – transaction may or may not be followed
TransactionFlowOption.Mandatory – transaction must be followed
TransactionFlowOption.NotAllowed – transaction not allowed
Example:
[OperationContract]
[TransactionFlow(TransactionFlowOption.Mandatory)]
void TransferAmountUsingSql(ulong amount);
4. Implement method with operation behaviour (set TransactionScopeRequire= True and TransactionAutoComplete=true) in interface as follows
By setting TransactionScopeRequire= True, we make the particular function can only be called with TransactionScope from client.
By setting TransactionAutoComplete=true, we make the transaction to commit automatically once its complete its process
Example:
[OperationBehavior(TransactionScopeRequired=true,TransactionAutoComplete=true)]
public void TransferAmountUsingSql(ulong amount)
{
using (SqlConnection conn=new SqlConnection("your Connection string here"))
{
using (SqlCommand cmd=new SqlCommand("your Cmd Text here",conn))
{
//your code logic here
cmd.ExecuteNonQuery();
}
}
}
5. Modify configuration file to allow transaction, by setting transactionFlow=”true”
<bindings>
<wsHttpBinding>
<binding name="transactionExample" transactionFlow="true"></binding>
</wsHttpBinding>
</bindings>
6. Create client application and consume WCF service as follows,
using (TransactionScope transScrope=new TransactionScope(TransactionScopeOption.Required))
{
try
{
ulong amount=Convert.ToUInt64(txtAmount.Value);
wcfTransaction.TransferAmountSuccess(amount);
lblResult.Text = "Succeed";
}
catch (Exception ex)
{
lblResult.Text = ex.Message;
}
finally
{
transScrope.Complete();
}
}
}
Project Description:
I enclosed sample project to demonstrate Transaction in WCF with self hosting
Download WCF Sample
No comments:
Post a Comment