Author : Gaurav Arora Reviewer : Meetu Choudhary Created on : Dec 20 th  , 2009 Website : http://www.hyderabatechies.info ...
About Author <ul><li>Gaurav Arora is Microsoft Certified Technical Specialist. He is M.Phill(Comp. Sc.), Masters in Comput...
<ul><li>Transaction Management </li></ul><ul><ul><ul><li>What is Transaction </li></ul></ul></ul><ul><ul><ul><li>Pure/Succ...
<ul><li>WCF Transaction in Action </li></ul><ul><ul><ul><li>Enabling Transaction </li></ul></ul></ul><ul><ul><ul><li>Creat...
<ul><li>  What is Transaction? </li></ul><ul><li>In simple words we can define a transaction as :Transaction is a group of...
<ul><li>The above task can be made happy to everyone if this contain some undo / rollback things, which means if due to so...
<ul><li>A transaction is said to be Pure and Successive, if </li></ul><ul><li>Transaction fulfill all the rules </li></ul>...
WCF Transaction Propagation <ul><li>Transactions are the key to building robust, high-quality service-oriented application...
WCF Transaction Attributes <ul><li>A new namespace  System.Transactions  was released in .Net2.0, which makes transaction ...
WCF Transaction Attributes <ul><li>There are three transaction attributes defined: </li></ul><ul><li>ServiceBehavior Attri...
ServiceBehavior Attribute <ul><li>The [ServiceBehavior] attribute has three properties that deal with handling and managin...
Example : Service Behavior Attribute <ul><li>Lets consider following piece of code: </li></ul><ul><li>  [ServiceBehavior(T...
OperationBehavior Attribute <ul><li>The [OperationBehavior] also has a couple of properties related to transactions. The t...
Example : OperationBehavior Attribute <ul><li>Following snippet tells how to use the OperationBehavior Attribute </li></ul...
TransactionFlow Attribute <ul><li>The [TransactionFlow] attribute is used to specify the level at which a service operatio...
Example : TransactionFlow Attribute <ul><li>TransactionFlowOption.Mandatory : This option specifies that client must call ...
Protocol to handle transaction in WCF <ul><li>Windows Communication Foundation utilizes the WS-AT (Web Services-Atomic Tra...
Pictorial view :  WS-AT protocol : in action 14
Phases in WCF Transaction <ul><li>WCF transactions follow 2 phase commit. So there are 2 phases one is the prepare phase a...
WCF Transaction phases in Action 16
Explaining WCF Transaction phases in Action <ul><li>Let’s consider 3 computers as shown in the previous slide. The client ...
Enabling WCF Transaction <ul><li>First of all, let it be cleared that Transaction do not flow by default. </li></ul><ul><l...
Step-I : Creation of services <ul><li>The first step is to create two WCF service projects which will participate in one t...
Step-II : Attribute Interface Methods with TransactionFlow <ul><li>In both the WCF services, we will create a method calle...
Step-III : Attribute the Implementation with TransactionScopeRequired <ul><li>The 3rd step is to attribute the implementat...
Step-IV : Enable Transaction Flow using WCF Service Config File <ul><li>We also need to enable transactions for wsHttpBind...
Step-V : Call the 2 Services in One Transaction <ul><li>Now that we are done with enabling our server side transaction, it...
Step 6 : Test does Transaction Work <ul><li>It’s time to test if the transactions really work. We are calling two services...
Step 6 : Test does Transaction Work 25
Points to Remember <ul><li>Transaction is a bunch of operations </li></ul><ul><li>A pure/successive transaction comply all...
Reference and Gratitude to <ul><li>Shiv Prasad Koirala </li></ul><ul><li>Chandershekhar Thota </li></ul><ul><li>Meetu Chou...
<ul><li>Thank you Hyderabad Techies </li></ul>Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions o...
Upcoming SlideShare
Loading in...5
×

Wcf Transaction Handling

6,084

Published on

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

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

Published in: Education, Technology, Real Estate
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,084
On Slideshare
0
From Embeds
0
Number of Embeds
9
Actions
Shares
0
Downloads
209
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • WCF Transaction handlings: Provided by HyderabadTechies©2009
  • Gaurav Arora has his blog at : http://stuff4mdesktop.blogspot.com
  • Wcf Transaction Handling

    1. 1. Author : Gaurav Arora Reviewer : Meetu Choudhary Created on : Dec 20 th , 2009 Website : http://www.hyderabatechies.info FROM HYDERABADTECHIES
    2. 2. About Author <ul><li>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 . </li></ul><ul><li>Gaurav is the co-founder of MsDotNetHeaven a free I.T. online educational web portal. </li></ul>Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
    3. 3. <ul><li>Transaction Management </li></ul><ul><ul><ul><li>What is Transaction </li></ul></ul></ul><ul><ul><ul><li>Pure/Successive transaction </li></ul></ul></ul><ul><ul><ul><li>Transaction Propagation </li></ul></ul></ul><ul><li>WCF Transaction Handling </li></ul><ul><ul><ul><li>Introduction of .Net Namespace </li></ul></ul></ul><ul><ul><ul><li>Transaction Attributes </li></ul></ul></ul><ul><ul><ul><li>ServiceBehavior Attribute </li></ul></ul></ul><ul><ul><ul><li>OperationBehavior Attribute </li></ul></ul></ul><ul><ul><ul><li>TransactionFlow Attribute </li></ul></ul></ul><ul><li>WCF Transaction Flow </li></ul><ul><ul><ul><li>WS-AT Protocol </li></ul></ul></ul><ul><ul><ul><li>Transaction Phases </li></ul></ul></ul>Contents Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
    4. 4. <ul><li>WCF Transaction in Action </li></ul><ul><ul><ul><li>Enabling Transaction </li></ul></ul></ul><ul><ul><ul><li>Creation of Services </li></ul></ul></ul><ul><ul><ul><li>Use of TransactionFlow </li></ul></ul></ul><ul><ul><ul><li>Implementation with TransactionScopeRequired </li></ul></ul></ul><ul><ul><ul><li>Enable Transaction Flow using WCF service Config File </li></ul></ul></ul><ul><ul><ul><li>Calling Services </li></ul></ul></ul><ul><ul><ul><li>Testing Services </li></ul></ul></ul>Contents Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
    5. 5. <ul><li> What is Transaction? </li></ul><ul><li>In simple words we can define a transaction as :Transaction is a group of various operations, executed as a bunch or whole. </li></ul><ul><li>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 ? </li></ul>Introduction Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
    6. 6. <ul><li>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. </li></ul><ul><li>Now, we can define Transaction as : </li></ul><ul><li>&quot;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.&quot; </li></ul>What is Transaction? Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
    7. 7. <ul><li>A transaction is said to be Pure and Successive, if </li></ul><ul><li>Transaction fulfill all the rules </li></ul><ul><li>Comply with its all characteristics known as ACID </li></ul><ul><li> A pure and successful transaction has four characteristics, also called as ACID: </li></ul><ul><li>A - Atomic </li></ul><ul><li>C - Consistent </li></ul><ul><li>I - Isolated </li></ul><ul><li>D - Durable </li></ul><ul><li>Note : We do not cover these characteristics in details, this session </li></ul>Pure and Successive Transaction Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
    8. 8. WCF Transaction Propagation <ul><li>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. </li></ul><ul><li>By - Juval Lowy </li></ul>4
    9. 9. WCF Transaction Attributes <ul><li>A new namespace System.Transactions was released in .Net2.0, which makes transaction programming easy and efficient. </li></ul><ul><li>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 </li></ul>5
    10. 10. WCF Transaction Attributes <ul><li>There are three transaction attributes defined: </li></ul><ul><li>ServiceBehavior Attribute </li></ul><ul><li>OperationBehavior Attribute </li></ul><ul><li>TransactionFlow Attribute </li></ul><ul><li>These all attributes have their own properties, which are helpful in transaction. </li></ul><ul><li>We will discuss all one-by-one </li></ul>6
    11. 11. ServiceBehavior Attribute <ul><li>The [ServiceBehavior] attribute has three properties that deal with handling and managing transactions. </li></ul><ul><li>TransactionAutoCompleteOnSessionClose: Specifies whether pending transactions are completed when the current session closes. </li></ul><ul><li>TransactionIsolationLevel: Determines the isolation level of the transaction. </li></ul><ul><li>TransactionTimeout: Specifies the period in which a transaction has to complete. </li></ul>7
    12. 12. Example : Service Behavior Attribute <ul><li>Lets consider following piece of code: </li></ul><ul><li> [ServiceBehavior(TransactionAutoCompleteOnSessionClose=true, </li></ul><ul><li>TransactionIsolationLevel=IsolationLevel.ReadCommitted, </li></ul><ul><li>TransactionTimeout=&quot;00:00:30&quot;)] </li></ul><ul><li>public class ArtNamesClass : IServiceClass </li></ul><ul><li>{ </li></ul><ul><li>[OperationBehavior] </li></ul><ul><li>string IServiceClass.FwtchName() </li></ul><ul><li>{ </li></ul><ul><li>StreamReader sw = new StreamReader(@&quot;E:shuby utorialarticlenames.txt&quot;); </li></ul><ul><li>return sw.ReadLine(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>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. </li></ul>8
    13. 13. OperationBehavior Attribute <ul><li>The [OperationBehavior] also has a couple of properties related to transactions. The two properties are: </li></ul><ul><li>TransactionAutoComplete: Specifies that transactions will be auto-completed if no exceptions occur. </li></ul><ul><li>TransactionScopeRequired: Specifies whether the associate method requires a transaction. </li></ul>9
    14. 14. Example : OperationBehavior Attribute <ul><li>Following snippet tells how to use the OperationBehavior Attribute </li></ul><ul><li>[OperationBehavior(TransactionAutoComplete=true, </li></ul><ul><li>TransactionScopeRequired=true)] </li></ul><ul><li>string IServiceClass.FwtchName() </li></ul><ul><li>{ </li></ul><ul><li>StreamReader sw = new StreamReader(@&quot;E:shuby utorialarticlenames.txt&quot;); </li></ul><ul><li>return sw.ReadLine(); </li></ul><ul><li>} </li></ul><ul><li>In this example, the TransactionAutoComplete property is set to true and the TransactionScopeRequired property is set to true as well. </li></ul>10
    15. 15. TransactionFlow Attribute <ul><li>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. </li></ul><ul><li>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. </li></ul><ul><li>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. </li></ul>11
    16. 16. Example : TransactionFlow Attribute <ul><li>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. </li></ul><ul><li>Consider following example: </li></ul><ul><li> [ServiceContract] </li></ul><ul><li>public interface IService1 </li></ul><ul><li>{ </li></ul><ul><li>[OperationContract] </li></ul><ul><li>[TransactionFlow(TransactionFlowOption.Allowed)] </li></ul><ul><li>void UpdateData(); </li></ul><ul><li>} </li></ul><ul><li>Above example tells the transaction may be followed while updating the data through UpdateData() method </li></ul>12
    17. 17. Protocol to handle transaction in WCF <ul><li>Windows Communication Foundation utilizes the WS-AT (Web Services-Atomic Transaction) protocol to flow transactions to other applications. </li></ul><ul><li>WS-AT ensures that if a single atomic transaction fails, the whole transaction fails: A partial transaction cannot take place. </li></ul><ul><li>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. </li></ul>13
    18. 18. Pictorial view : WS-AT protocol : in action 14
    19. 19. Phases in WCF Transaction <ul><li>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. </li></ul><ul><li>In prepare phase the transaction manager checks whether all entities are prepared to commit. In commit phase the actual commit starts. </li></ul>15
    20. 20. WCF Transaction phases in Action 16
    21. 21. Explaining WCF Transaction phases in Action <ul><li>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. </li></ul>17
    22. 22. Enabling WCF Transaction <ul><li>First of all, let it be cleared that Transaction do not flow by default. </li></ul><ul><li>Enabling WCF Transactions are as easy as to follow these 6-steps: </li></ul><ul><li>Creation of 2-WCF services </li></ul><ul><li>Attribute interface Methods with TransactionalFlow </li></ul><ul><li>Attribute the Implementation with TransactionScopeRequired </li></ul><ul><li>Enable Transaction Flow using WCF Service Config File </li></ul><ul><li>Call the 2 Services in One Transaction </li></ul><ul><li>Testing, by throwing Forcefuly exception </li></ul>18
    23. 23. Step-I : Creation of services <ul><li>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. </li></ul>19
    24. 24. Step-II : Attribute Interface Methods with TransactionFlow <ul><li>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. </li></ul><ul><li>[ServiceContract] </li></ul><ul><li>public interface IService1 </li></ul><ul><li>{ </li></ul><ul><li>[OperationContract] </li></ul><ul><li>[TransactionFlow(TransactionFlowOption.Allowed)] </li></ul><ul><li>void UpdateData(); </li></ul><ul><li>} </li></ul>20
    25. 25. Step-III : Attribute the Implementation with TransactionScopeRequired <ul><li>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. </li></ul><ul><li>[OperationBehavior(TransactionScopeRequired = true)] </li></ul><ul><li>public void UpdateData() </li></ul><ul><li>{ </li></ul><ul><li>SqlConnection objConnection = new SqlConnection(strConnection); </li></ul><ul><li>objConnection.Open(); </li></ul><ul><li>SqlCommand objCommand = new SqlCommand(&quot;insert into Customer </li></ul><ul><li>(CustomerName,CustomerCode) values('sss','sss')&quot;,objConnection); </li></ul><ul><li>objCommand.ExecuteNonQuery(); </li></ul><ul><li>objConnection.Close(); </li></ul><ul><li>} </li></ul>21
    26. 26. Step-IV : Enable Transaction Flow using WCF Service Config File <ul><li>We also need to enable transactions for wsHttpBinding by setting the transactionFlow attribute to true. </li></ul><ul><li><bindings> </li></ul><ul><li><wsHttpBinding> </li></ul><ul><li><binding name=&quot;TransactionalBind&quot; transactionFlow=&quot;true&quot;/> </li></ul><ul><li></wsHttpBinding> </li></ul><ul><li></bindings> </li></ul><ul><li>The transaction enabled binding we need to attach with the end point through which our WCF service is exposed. </li></ul><ul><li><endpoint address=&quot;&quot; binding=&quot;wsHttpBinding&quot; </li></ul><ul><li>bindingConfiguration=&quot;TransactionalBind&quot; contract=&quot;WcfService1.IService1&quot;> </li></ul>22
    27. 27. Step-V : Call the 2 Services in One Transaction <ul><li>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. </li></ul>23
    28. 28. Step 6 : Test does Transaction Work <ul><li>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. </li></ul>24
    29. 29. Step 6 : Test does Transaction Work 25
    30. 30. Points to Remember <ul><li>Transaction is a bunch of operations </li></ul><ul><li>A pure/successive transaction comply all characteristics known as ACID </li></ul><ul><li>The three transaction attributes are : </li></ul><ul><ul><li>ServiceBehavior Attribute </li></ul></ul><ul><ul><li>OperationBehavior Attribute </li></ul></ul><ul><ul><li>TransactionFlow Attribute </li></ul></ul><ul><li>TransactionFlow.NotAllowed is a default option of Attribute TransactionFlow. </li></ul><ul><li>WCF utilizes WS-AT protocol to flow transactions. </li></ul><ul><li>WCF transactions follow 2-phase commit </li></ul><ul><li>WCF transactions are not possible with 1-way WCF service call </li></ul>26
    31. 31. Reference and Gratitude to <ul><li>Shiv Prasad Koirala </li></ul><ul><li>Chandershekhar Thota </li></ul><ul><li>Meetu Choudhary </li></ul><ul><li>Jullius Mourice [Abit Smart] </li></ul><ul><li>MSDN </li></ul><ul><li>CodeProject </li></ul><ul><li>Dotnetspider </li></ul><ul><li>Msdotnetmentor </li></ul><ul><li>Interview.msdotnetheaven </li></ul>46
    32. 32. <ul><li>Thank you Hyderabad Techies </li></ul>Microsoft Developer User Group - Hyderabad :: Free Online/Offline Sessions on Microsoft Technologies
    1. ¿Le ha llamado la atención una diapositiva en particular?

      Recortar diapositivas es una manera útil de recopilar información importante para consultarla más tarde.

    ×