Instances of a .NET Framework class can participate in an automatic transaction, as long as you prepare the class to do so. Each resource accessed by a class instance, or object, enlists in the transaction. For example, if an object uses ADO. Net to Post Money On An Account in A Database, The Resource Manager for the Database Determines WHETER THE OBALD EX, ITAMAATILE ENLISTS The Database In The Transaction.
Use the fol supply process to prepare a class to participate in an Automatic Transaction:
Apply the
TransactionAttribute
To your class. derive your class from the
ServicedComponent Class
.. Sign the assembly with a strong name To sign the assembly using attributes, create a key pair using Sn.exe: sn -k TestApp.snk Add the AssemblyKeyFileAttribute or AssemblyKeyNameAttribute assembly attribute specifying the name of the file containing the key pair to sign The assembly with a strong name. [Visual Basic]
.. [Assembly: AssemblyKeyFileAttribute ( "TestApp.snk")] For details see Signing an Assembly with a Strong Name Register the assembly that contains your class with the COM catalog If the client calling instances of your class is managed by the common language runtime , the registration ispeformed for you. However, if you antipate That An Unmanaged Caller Might Create and Call Instances of Your Class, Use T
.NET Services Installation Tool (Regsvcs.exe)
TO Perform The Registration Manually.
The Following Example Shows How To Apply The TransactionAttribute Attribute To a Class Derived from The ServicComponent Class.
[Visual Basic]
'.
END CLASS
[C #]
[Transaction (TransactionOption.Required)]
Public class samples (): servicedcomponent
{
//.
}
When applying the transaction attribute, you can use Transaction, transaction, TransactionAttribute, and transactionattribute interchangeably. For example, you can use either Transaction or transactionattribute to produce identical results.
The Following Table Lists and Describes Each Constructor Variation.
Attribute valueDescriptionDisabledEliminates the control of automatic transactions on the object. An object with this attribute value applied can engage the Distributed Transaction Coordinator (DTC) directly for transactional support. [Transaction (TransactionOption.Disabled)] NotSupportedIndicates that the object does not run within the scope of transactions. When a request is processed, its object context is created without a transaction, regardless of whether there is a transaction active. [Transaction (TransactionOption.NotSupported)] SupportedIndicates that the object runs in the context of an existing transaction, if one exists. If no transaction exists, the object runs without a transaction. [Transaction (TransactionOption.Supported)] Required (default) Indicates that the object requires a transaction. It runs in the scope of an existing transaction, if one exists. If no Transaction Exists, The Object Starts One. [Transaction (TransactionOption.Required] Requiresnewindicates That Th T E Object Requires a Transaction and a new transaction is started for each request. [Transaction (TransactionOption.Requiresnew]
Sample class
THE FOLALAL Elements of an Automatic Transaction. In this Example, Both the Transactional Class and The Client That Calls The Class Are Management by the runtime. [Visual Basic]
'------------------------------------- ----------------
'TestApp.vb
'Generate A Strong Name:
'Sn -k Testapp.snk
'Compile the code:
'VBC / Target: Exe /R:System.EnterpriseServices.dll TestApp.vb
'Run TestApp:
'Start Testapp.exe
'------------------------------------- ----------------
Option expedition
Option strict
Imports system
Imports system.Runtime.compilerServices
Imports System.EnterpriseServices
Imports system.reflection
'Registration details.
'COM Application Name as it Appears in the COM Catalog.
'Strong name for assembly.
Inherits ServicedComponent
'Provides setcomplete behavior in the absence of exceptions.
'Do Some Database Work. Any Exception Thrown Here Aborts
'Transaction; Otherwise, Transaction Commits.
End Sub
END CLASS
Public Class Client
Public Shared Sub Main ()
DIM AccountX as new account ()
Accountx.debit (100)
Environment.exit (0)
End Sub
END CLASS
[C #]
/ / -------------------------------------------------------------------------------------------- -----------------
// TestApp.cs
// generate a strong name:
// sn -k testapp.snk
// compile the code:
// CSC / Target: Exe /R :system.EnterpriseServices.dll TestApp.cs
// Run TestApp:
// Start TestApp.exe // ------------------------------------------ -----------------------
Using system;
Using system.Runtime.compilerServices;
Using system.enterprises;
Using system.reflection;
// registration details.
// COM Application Name as it Appears in the COM Catalog.
[Assembly: ApplicationName ("TestApp")]]]
// Strong name for assembly.
[Assembly: AssemblyKeyFileAttribute ("testapp.snk")]]
[Transaction (TransactionOption.Required)]
Public Class Account: ServicedComponent
{
// provides setcomplete behavior in the absence of exceptions.
[AutoComplete]
Public void debit (int Amount)
{
// do some database work. Any Exception thrown here Aborts the
// Transaction; OtherWise, Transaction Commits.
}
}
Public Class Client
{
Public static int main ()
{
Account AccountX = New Account ();
AccountX.debit (100);
Return 0;
}
}