• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Wcf Transaction Handling
 

Wcf Transaction Handling

on

  • 7,413 views

This is the presentation of my session presented for Hyderabad Techies.

This is the presentation of my session presented for Hyderabad Techies.

I must pay my thanks to Shiv Prasad Koirala and Chandershekhar Thota :)

Statistics

Views

Total Views
7,413
Views on SlideShare
7,115
Embed Views
298

Actions

Likes
1
Downloads
195
Comments
0

20 Embeds 298

http://www.wiziq.com 147
http://aspnetbymeetu.blogspot.com 53
http://www.slideshare.net 45
http://aspnetbymeetu.blogspot.in 17
http://stuff4mdesktop.blogspot.com 10
http://www.msdotnetheaven.com 5
http://www.linkedin.com 4
http://www.stuff4mdesktop.blogspot.com 3
http://stuff4mdesktop.blogspot.in 2
http://msdotnetheaven.com 2
http://gaurav-arora.com 1
http://www.stuff4mdesktop.blogspot.ca 1
http://stuff4mdesktop.blogspot.ie 1
http://aspnetbymeetu.blogspot.tw 1
http://translate.googleusercontent.com 1
http://aspnetbymeetu.blogspot.ca 1
http://aspnetbymeetu.blogspot.mx 1
http://aspnetbymeetu.blogspot.hu 1
http://www.lmodules.com 1
https://www.linkedin.com 1
More...

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • WCF Transaction handlings: Provided by HyderabadTechies©2009
  • Gaurav Arora has his blog at : http://stuff4mdesktop.blogspot.com

Wcf Transaction Handling Wcf Transaction Handling Presentation Transcript

  • Author : Gaurav Arora Reviewer : Meetu Choudhary Created on : Dec 20 th , 2009 Website : http://www.hyderabatechies.info FROM HYDERABADTECHIES
  • About Author
    • Gaurav Arora is Microsoft Certified Technical Specialist. He is M.Phill(Comp. Sc.), Masters in Computer Applications. Gaurav is an author of various online/offline articles, tutorials, including Silverlight, Asp.net, WCF, LINQ etc. He is the Founder, Webmaster and Sr. Editor of Technical communities viz. Silverlight Club and Dotnetspider .
    • Gaurav is the co-founder of MsDotNetHeaven a free I.T. online educational web portal.
    Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
    • Transaction Management
        • What is Transaction
        • Pure/Successive transaction
        • Transaction Propagation
    • WCF Transaction Handling
        • Introduction of .Net Namespace
        • Transaction Attributes
        • ServiceBehavior Attribute
        • OperationBehavior Attribute
        • TransactionFlow Attribute
    • WCF Transaction Flow
        • WS-AT Protocol
        • Transaction Phases
    Contents Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
    • WCF Transaction in Action
        • Enabling Transaction
        • Creation of Services
        • Use of TransactionFlow
        • Implementation with TransactionScopeRequired
        • Enable Transaction Flow using WCF service Config File
        • Calling Services
        • Testing Services
    Contents Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
    • What is Transaction?
    • In simple words we can define a transaction as :Transaction is a group of various operations, executed as a bunch or whole.
    • In detailed way you can think an online shopping cart where one purchased some accessories and made the payment and both the vendor and customer get debited viz. product and amount, now suppose during this exercise if connection fails then what happened, no one happy ?
    Introduction Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
    • The above task can be made happy to everyone if this contain some undo / rollback things, which means if due to some avoidable/un-avoidable reasons connection breaks / fails then all entries would be marked as reversed. This bunch of collective operation is nothing but the transaction.
    • Now, we can define Transaction as :
    • "Transaction is a collection or group of one or more units of operation executed as a whole. Another way to say it is that transactions provide a way to logically group single pieces of work and execute them as a single unit, or transaction."
    What is Transaction? Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
    • A transaction is said to be Pure and Successive, if
    • Transaction fulfill all the rules
    • Comply with its all characteristics known as ACID
    • A pure and successful transaction has four characteristics, also called as ACID:
    • A - Atomic
    • C - Consistent
    • I - Isolated
    • D - Durable
    • Note : We do not cover these characteristics in details, this session
    Pure and Successive Transaction Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
  • WCF Transaction Propagation
    • Transactions are the key to building robust, high-quality service-oriented applications. Windows® Communication Foundation provides simple, declarative transaction support for service developers, enabling you to configure parameters such as transaction flow and voting, all outside the scope of your service. In addition, Windows Communication Foundation allows client applications to create transactions and to propagate them across service boundaries.
    • By - Juval Lowy
    4
  • WCF Transaction Attributes
    • A new namespace System.Transactions was released in .Net2.0, which makes transaction programming easy and efficient.
    • Windows Communication Foundation utilizes the many available objects of this namespace to provide all the necessary transaction capabilities you will need when building your WCF services and client applications
    5
  • WCF Transaction Attributes
    • There are three transaction attributes defined:
    • ServiceBehavior Attribute
    • OperationBehavior Attribute
    • TransactionFlow Attribute
    • These all attributes have their own properties, which are helpful in transaction.
    • We will discuss all one-by-one
    6
  • ServiceBehavior Attribute
    • The [ServiceBehavior] attribute has three properties that deal with handling and managing transactions.
    • TransactionAutoCompleteOnSessionClose: Specifies whether pending transactions are completed when the current session closes.
    • TransactionIsolationLevel: Determines the isolation level of the transaction.
    • TransactionTimeout: Specifies the period in which a transaction has to complete.
    7
  • Example : Service Behavior Attribute
    • Lets consider following piece of code:
    • [ServiceBehavior(TransactionAutoCompleteOnSessionClose=true,
    • TransactionIsolationLevel=IsolationLevel.ReadCommitted,
    • TransactionTimeout="00:00:30")]
    • public class ArtNamesClass : IServiceClass
    • {
    • [OperationBehavior]
    • string IServiceClass.FwtchName()
    • {
    • StreamReader sw = new StreamReader(@"E:shuby utorialarticlenames.txt");
    • return sw.ReadLine();
    • }
    • }
    • In this example, the TransactionAutoCompleteOnSessionClose property is set to true, the TransactionIsolationLevel is set to ReadCommitted, and the TransactionTimeout is set to 30 seconds. The TransactionTimeout property value is of a Timespan object.
    8
  • OperationBehavior Attribute
    • The [OperationBehavior] also has a couple of properties related to transactions. The two properties are:
    • TransactionAutoComplete: Specifies that transactions will be auto-completed if no exceptions occur.
    • TransactionScopeRequired: Specifies whether the associate method requires a transaction.
    9
  • Example : OperationBehavior Attribute
    • Following snippet tells how to use the OperationBehavior Attribute
    • [OperationBehavior(TransactionAutoComplete=true,
    • TransactionScopeRequired=true)]
    • string IServiceClass.FwtchName()
    • {
    • StreamReader sw = new StreamReader(@"E:shuby utorialarticlenames.txt");
    • return sw.ReadLine();
    • }
    • In this example, the TransactionAutoComplete property is set to true and the TransactionScopeRequired property is set to true as well.
    10
  • TransactionFlow Attribute
    • The [TransactionFlow] attribute is used to specify the level at which a service operation can accept a transaction header. This attribute has a single property and is the attribute used to annotate a service operation method.
    • TransactionFlowOption.NotAllowed : This is a default option. Using this option no transaction will be propagated across the binding. If any client attempts to call the WCF service in a transaction it will be ignored for this option.
    • TransactionFlowOption.Allowed : This option specifies that client can call this WCF service in a transaction. It’s not compulsory that the service needs to be called in a transaction. You can call without the transaction also.
    11
  • Example : TransactionFlow Attribute
    • TransactionFlowOption.Mandatory : This option specifies that client must call the WCF service in a transaction mode. If the WCF service is called without transaction, ‘FaultException’ will be raised.
    • Consider following example:
    • [ServiceContract]
    • public interface IService1
    • {
    • [OperationContract]
    • [TransactionFlow(TransactionFlowOption.Allowed)]
    • void UpdateData();
    • }
    • Above example tells the transaction may be followed while updating the data through UpdateData() method
    12
  • Protocol to handle transaction in WCF
    • Windows Communication Foundation utilizes the WS-AT (Web Services-Atomic Transaction) protocol to flow transactions to other applications.
    • WS-AT ensures that if a single atomic transaction fails, the whole transaction fails: A partial transaction cannot take place.
    • For example, if a customer is transferring funds from one account to another, WS-AT makes it impossible for funds to be removed from one account successfully but not deposited into the target account.
    13
  • Pictorial view : WS-AT protocol : in action 14
  • Phases in WCF Transaction
    • WCF transactions follow 2 phase commit. So there are 2 phases one is the prepare phase and the other is the commit phase. All co-ordination of transactions is done by the transaction manager.
    • In prepare phase the transaction manager checks whether all entities are prepared to commit. In commit phase the actual commit starts.
    15
  • WCF Transaction phases in Action 16
  • Explaining WCF Transaction phases in Action
    • Let’s consider 3 computers as shown in the previous slide. The client consuming the WCF service resides in computer ‘A’ while computer ‘B’ and ‘C’ have the WCF services. The transaction is initiated from the computer ‘A’. So as we already discussed previously there are 2 phase one is the prepare phase and the other commit phase. In prepare phase computer ‘A’ sends messages to all the WCF services saying, are they ready to commit?. Once all WCF services respond saying that they are ready for commit it starts the second phase i.e. In the second phase the WCF client issues a commit command. All the WCF services start execution and once they are done they revert back saying they have committed. When all the WCF services revert saying they have committed the transaction is marked as successful.
    17
  • Enabling WCF Transaction
    • First of all, let it be cleared that Transaction do not flow by default.
    • Enabling WCF Transactions are as easy as to follow these 6-steps:
    • Creation of 2-WCF services
    • Attribute interface Methods with TransactionalFlow
    • Attribute the Implementation with TransactionScopeRequired
    • Enable Transaction Flow using WCF Service Config File
    • Call the 2 Services in One Transaction
    • Testing, by throwing Forcefuly exception
    18
  • Step-I : Creation of services
    • The first step is to create two WCF service projects which will participate in one transaction. In both of these WCF services, we will do database transactions and we will try to understand how a WCF transaction unifies them. We have also created a web application with name WCFTransactions which will consume both the services in one transaction scope.
    19
  • Step-II : Attribute Interface Methods with TransactionFlow
    • In both the WCF services, we will create a method called UpdateData which will insert into the database. So the first thing is to create the interface class with ServiceContract attribute and the method UpdateData with OperationContract attribute. In order to enable transaction in UpdateData method, we need to attribute it with TransactionFlow and we have specified that transactions are allowed for this method using TransactionFlowOption.Allowed enum.
    • [ServiceContract]
    • public interface IService1
    • {
    • [OperationContract]
    • [TransactionFlow(TransactionFlowOption.Allowed)]
    • void UpdateData();
    • }
    20
  • Step-III : Attribute the Implementation with TransactionScopeRequired
    • The 3rd step is to attribute the implementation of the WCF services with TransactionScopeRequired as true. Below is the code snippet which has a simple database inserting function, i.e. UpdateData which is attributed by TransactionScopeRequired attribute.
    • [OperationBehavior(TransactionScopeRequired = true)]
    • public void UpdateData()
    • {
    • SqlConnection objConnection = new SqlConnection(strConnection);
    • objConnection.Open();
    • SqlCommand objCommand = new SqlCommand("insert into Customer
    • (CustomerName,CustomerCode) values('sss','sss')",objConnection);
    • objCommand.ExecuteNonQuery();
    • objConnection.Close();
    • }
    21
  • Step-IV : Enable Transaction Flow using WCF Service Config File
    • We also need to enable transactions for wsHttpBinding by setting the transactionFlow attribute to true.
    • <bindings>
    • <wsHttpBinding>
    • <binding name=&quot;TransactionalBind&quot; transactionFlow=&quot;true&quot;/>
    • </wsHttpBinding>
    • </bindings>
    • The transaction enabled binding we need to attach with the end point through which our WCF service is exposed.
    • <endpoint address=&quot;&quot; binding=&quot;wsHttpBinding&quot;
    • bindingConfiguration=&quot;TransactionalBind&quot; contract=&quot;WcfService1.IService1&quot;>
    22
  • Step-V : Call the 2 Services in One Transaction
    • Now that we are done with enabling our server side transaction, it’s time to call the above 2 services in 1 transaction. We need to use the TransactionScope object to group the above 2 WCF services in one transaction. To commit all the WCF transactions, we call the Complete method of the Transactionscope object. To rollback, we need to call the Dispose method.
    23
  • Step 6 : Test does Transaction Work
    • It’s time to test if the transactions really work. We are calling two services, both of which are doing an insert. After the first WCF service call, we are forcing an exception. In other words, the data insert of the first WCF service should revert back. If you check the database records, you will see no records are inserted by the WCF service.
    24
  • Step 6 : Test does Transaction Work 25
  • Points to Remember
    • Transaction is a bunch of operations
    • A pure/successive transaction comply all characteristics known as ACID
    • The three transaction attributes are :
      • ServiceBehavior Attribute
      • OperationBehavior Attribute
      • TransactionFlow Attribute
    • TransactionFlow.NotAllowed is a default option of Attribute TransactionFlow.
    • WCF utilizes WS-AT protocol to flow transactions.
    • WCF transactions follow 2-phase commit
    • WCF transactions are not possible with 1-way WCF service call
    26
  • Reference and Gratitude to
    • Shiv Prasad Koirala
    • Chandershekhar Thota
    • Meetu Choudhary
    • Jullius Mourice [Abit Smart]
    • MSDN
    • CodeProject
    • Dotnetspider
    • Msdotnetmentor
    • Interview.msdotnetheaven
    46
    • Thank you Hyderabad Techies
    Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies