WCF Transactions


Published on

In-depth presentation about Transactions in WCF

Published in: Technology, Real Estate
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Transactions are necessary when two or more activities must be coordinated as a single operation, ensuring that the system is always left in a consistent state.

    The only two viable options are to complete all activities successfully or to leave the system as it was before initiating either activity.
  • The System.Transactions namespace is introduced with the .NET Framework 2.0

    By default, what this block of code does is construct a new transaction, or join an existing transaction if one already exists by an upstream caller.
  • The Microsoft Distributed Transaction Coordinator (DTC) is a transaction manager that is able to coordinate resource managers that span application domain, process, and machine boundaries on the same network.

    With .NET 2.0, the LTM will first promote to KTM based on the conditions discussed in the previous section but will promote to DTC if any of the following conditions are met:

  • Configure DTC Security

    Click Start, click Run, type dcomcnfg and then click OK to open Component Services.

    Enable Network DTC Access.

    For the most secure results, enable mutual authentication for inbound or outbound communication.

    For testing purposes, you may disable authentication initially. If authentication is required, certificates must be configured for WS-AT
  • 2. Then, from the WS-AT tab of the DTC
    Enable WS-AtomicTransaction network support.
    First, you must add the snap-in using the following command-line instruction:
    Regasm.exe /codebase wsatui.dll
    C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Regasm.exe /codebase "c:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\wsatui.dll"
    Once this is done, you'll be able to see a WS-AT tab added to the DTC properties dialog This is where you will enable WS-AT support on the machine.

    Configure the HTTPS port to be used.
    Select an endpoint certificate and be sure that this certificate matches the machine name.
  • A service-oriented enterprise system typically includes many tiers, including clients, services, business components, data access tiers, and of course the actual data store that usually includes stored procedures invoked by the data access tier.
    Transactions can be initiated at any of these tiers, depending on the application scenario.
    Figure 6-13. Transactions initiated at the (A) stored procedure; (B) data access tier; (C) business tier; (D) client (crossing service boundaries)

  • WCF Transactions

    1. 1. www.orbitone.com Raas van Gaverestraat 83 B-9000 GENT, Belgium E-mail info@orbitone.com Website www.orbitone.com Tel. +32 9 265 74 20 Fax +32 9 265 74 10 VAT BE 456.457.353 Bank 442-7059001-50 (KBC) 27 April, 2009 WCF Transactions by Tom Pester
    2. 2. 27 April, 2009 WCF Transactions, by Tom Pester2 ACID Transactions must have the ACID properties. ACID stands for: o Atomic o Consistent o Isolated o Durable
    3. 3. 27 April, 2009 WCF Transactions, by Tom Pester3 Two-phase commit protocol Phase 1 The coordinator asks each resource manager to prepare to commit. Each resource manager responds (votes) to commit or abort the transaction. The coordinator collects all votes and makes a decision to commit or abort the entire transaction.
    4. 4. 27 April, 2009 WCF Transactions, by Tom Pester4 Two-phase commit protocol Phase 2 The coordinator asks each resource manager to commit or abort based on this decision. If the resource manager is asked to commit, it acknowledges completion of the activity. If asked to abort, it rolls back the activity. The coordinator waits for acknowledgment from all resource managers that the transaction was successfully committed.
    5. 5. 27 April, 2009 WCF Transactions, by Tom Pester5 TransactionScope using (TransactionScope scope = new TransactionScope( )) { Operation1( ); Operation2( ); scope.Complete( ); }
    6. 6. 27 April, 2009 WCF Transactions, by Tom Pester6 Lightweight Transaction Manager  Any number of volatile resource managers can be enlisted.  Only a single durable resource manager can be enlisted.  No application domain or process boundaries can be crossed.
    7. 7. 27 April, 2009 WCF Transactions, by Tom Pester7 Kernel Transaction Manager  Windows Vista introduced the Kernel Transaction Manager (KTM) to manage the resource managers associated with its Transactional Registry (TxR) and its transaction filesystem, called Transactional NTFS (TxF).  When the downstream code within a TransactionScope enlists one of these resource managers, the LTM is promoted to KTM automatically. o Any number of volatile resource managers can be enlisted. o Only a single durable or kernel resource manager can be enlisted. o No application domain or process boundaries can be crossed.
    8. 8. 27 April, 2009 WCF Transactions, by Tom Pester8 Distributed Transaction Coordinator o An application, process, or machine boundary is crossed. o More than one durable resource manager is enlisted
    9. 9. 27 April, 2009 WCF Transactions, by Tom Pester9 WS-AtomicTransaction and WS-Coordination protocols  WS-AtomicTransaction (WS-AT) and WS-Coordination (WS-COOR) are interoperable protocols that enable message-based distributed transactions over HTTP and across platform boundaries
    10. 10. 27 April, 2009 WCF Transactions, by Tom Pester10 Enabling WS-AT
    11. 11. 27 April, 2009 WCF Transactions, by Tom Pester11 Regasm.exe /codebase wsatui.dll
    12. 12. 27 April, 2009 WCF Transactions, by Tom Pester12 Transactions and System Tiers
    13. 13. 27 April, 2009 WCF Transactions, by Tom Pester13 Configuring WCF Transactions Transaction flow between clients and services is a function of othe binding configuration, othe service contract requirements oand the behavior of the service operation being invoked.
    14. 14. 27 April, 2009 WCF Transactions, by Tom Pester14 Enabling transaction flow  For a client transaction to flow across a service boundary, the binding must support transaction flow, which immediately limits you to one of these standard bindings: NetNamedPipeBinding oNetTcpBinding oWSHttpBinding, oWSDualHttpBinding oand WSFederationHttpBinding. <wsHttpBinding> <binding name="wsHttpTxTransactionFlow="true" /> </wsHttpBinding>
    15. 15. 27 April, 2009 WCF Transactions, by Tom Pester15 Transaction flow options The TransactionFlowAttribute oNotAllowed oMandatory oAllowed [ServiceContract()] public interface ICountersService { [OperationContract] [TransactionFlow(TransactionFlowOption.Allowed)] void ResetCounters( );
    16. 16. 27 April, 2009 WCF Transactions, by Tom Pester16 Service operations and transactions  Although the TransactionFlowOption does influence the presence of a client transaction, it does not guarantee that a service operation will use the transaction.  In the implementation of a service contract, each operation must opt-in to support transactions. TransactionScopeRequired property of the OperationBehaviorAttribute [OperationBehavior(TransactionScopeRequired=true)] public void ResetCounters( ) {...}
    17. 17. 27 April, 2009 WCF Transactions, by Tom Pester17 TransactionScopeRequired  When set to false, which is the default setting, the service will never join a client transaction, nor will it automatically create a new transaction. That means that if the service code requires a transaction, it would have to do so manually using a TransactionScope block.  When set to true, if a client transaction is flowed to the service, the service operation will join that transaction. If a client transaction is not flowed, a new transaction is created for the service operation to execute in.
    18. 18. 27 April, 2009 WCF Transactions, by Tom Pester18 DistributedIdentifier  An indicator that the service is participating in the client transaction is when the DistributedIdentifier of the current transaction is set:  Transaction.Current.TransactionInformation.DistributedIdentifier
    19. 19. 27 April, 2009 WCF Transactions, by Tom Pester19 TransactionAutoComplete  Another property of the OperationBehaviorAttribute that affects how transactions are handled is the TransactionAutoComplete property. By default, this property is set to true, which means that the consistency bit is set to true automatically if no exceptions are thrown  [OperationBehavior(TransactionScopeRequired=true,TransactionAutoCompl ete=false)]
    20. 20. 27 April, 2009 WCF Transactions, by Tom Pester20 Controlling IsolationLevel  The System.Transactions namespace defines an IsolationLevel enumeration that defines the following isolation options o Chaos o ReadCommitted o ReadUncommitted o RepeatableRead o Serializable o Snapshot o Unspecified  The last of these settings, Unspecified, is the default.
    21. 21. 27 April, 2009 WCF Transactions, by Tom Pester21 TransactionOptions TransactionOptions options = new TransactionOptions( );options.IsolationLevel = IsolationLevel.Serializable; using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options)) { m_proxy.SetCounter1(int.Parse(this.txtCounter.Text)); m_proxy.SetCounter2(int.Parse(this.txtCounter.Text)); scope.Complete( ); }
    22. 22. www.orbitone.com 22 WCF Transactions, by Tom Pester 27 April, 2009