WCF Transactions-Part I

Posted: August 20, 2008 in Transactions, WCF
Tags: ,

In this post I will discuss about the basics of implementing a transaction aware WCF Service.Like many other features of WCF a transaction aware service can be very easily implemented using the declarative programming model and configuration settings.
The important attributes related to transaction are:

TransactionFlow

This attribute determines how a transaction should flow from client to the service.The three options specified by the TransactionFlowOption enumeration are
1)Mandatory
This makes transaction flow from client mandatory.If there is no transaction flowing from the client then an exception will be thrown.Making TransactionFlow mandatory for a service operation makes the service too tightly coupled with the client as clients without a transaction will never be able to use this method.
2)Allowed
This indicates that a transaction may or not flow from the client and this is the most flexible of the three options.
3)Not Allowed
This indicates any client transaction flowing will not be allowed and an exception will be thrown.

To create a transaction aware service we have to make the TransactionFlow attribute of the service operation mandatory/allowed:

[TransactionFlow(TransactionFlowOption.Allowed)]  
[TransactionFlow(TransactionFlowOption.Mandatory)]

We also have set the transactionFlow attribute of the binding in the configuration to true

<wsHttpBinding>
 <binding name=”wsHttp” transactionFlow=”true” />
</wsHttpBinding>

These settings needs to be applied both at the client as well as server end.

TransactionScope parameter of OperationBehavior Attribute

This parameter of OperationBehavior attribute if set to true then the service will join the transaction flowing from the client else it will create a new transaction.If set to false then it will never participate in a transaction.The default value is false.

TransactionAutocomplete parameter of OperationBehavior Attribute


This parameter if set to true the transaction will complete if the operation executes without any exception.By default this is set to true.If  it is false then the transaction completion has to be done programmatically using the SetTransactionComplete method of the OperationContext class.

We need to perform the following steps to make a service transaction aware:
Server Side

  1. Set TransactionFlow attribute to Allowed/Mandatory.
  2. Set transactionFlow attribute of the binding in the configuration to true.
  3. Set the TransactionScope to true and TransactionAutocomplete to true.

Client side

  1. Set TransactionFlow attribute to Allowed/Mandatory in the generated proxy class.
  2. Set transactionFlow attribute of the binding in the configuration to true.
  3. Invoke the service operation within a TransactionScope

In the next post I will discuss about the TransactionIsolation/Timeout and Session.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s