Reference:. Http://blogs.msdn.com/florinlazar/archive/2004/07/24/194199.aspxThe most used feature of System.EnterpriseServices or COM is the distributed transaction support And the automatic transaction programming model in ES using attributes ([Transaction] and [AutoComplete]) is great and nice but (it is always a but!) ... you need to inherit from ServicedComponent and the Transaction attribute is only available at class level, and you need to register your component in The COM Repository and The List Can Continue.
If doing this seems overkill to you, because all you need is a distributed transaction to protect your code / actions and you do not care of any of the others ES features (which are great ones nevertheless) then there is a solution for you: System.EnterpriseServices.ServiceDomain. Here is some sample code:
Using system;
Using system.enterprises;
Namespace SDSAMPLE
{
Class class1
{
[Mtathread]
Static void
Main
(String [] ARGS)
{
ServiceConfig CONFIG = New ServiceConfig ();
CONFIG.TRANSACTION = TransactionOption.Required;
ServiceDomain.enter (config);
Try
{
Mytxcode ();
}
Catch (Exception E)
{
// We got an exception
Console.writeLine (E.MESSAGE);
// So, Wehiba
Contextutil.Setabort ();
}
Finally
{
ServiceDomain.Leave ();
}
}
// the code what I want to be transactions
Static void mytxcode ()
{
Console.writeline (ContextUtil.Transaction);
// open connection to database 1
// Execute Update in Database 1
// Open connection to database 2
// Execute Update in Database 2
}
}
}
Of course, you can go further and create a helper class, let's call it ESTransactionScope (similar to System.Transactions.TransactionScope that will arrive in Whidbey) that will be very easy to use: using System;
Using system.enterprises;
Namespace SDSample2
{
Class class1
{
[Mtathread]
Static void
Main
(String [] ARGS)
{
Using (estransactionscope ts = new estranscope ())
{
Mytxcode ();
// Everything Went Well, No Exception Thrown
// So let's vote for commit
Ts.comPlete ();
}
}
Static void mytxcode ()
{
Console.writeline (ContextUtil.Transaction);
// open connection to database 1
// Execute Update in Database 1
// Open connection to database 2
// Execute Update in Database 2
}
}
// used to create Transactional Code Blocks
Class Estranscope: IDisposable
{
// Dispose Must Be Called to EXIT The Transactionsal Block
Public void dispose ()
{
IF (! this.constent)
{
Contextutil.Setabort ();
}
ServiceDomain.Leave ();
} // by calling this method, you mark the scope as being consistent // and reading to for commit //ness is never called, upon dispose
public
Void Complete ()
{
THIS.CONSISTENT =
True;
}
Public estransactionscope ()
{
EntertxContext (TransactionOption.Required);
}
Public estransactionscope (TransactionOption TXOption)
{
EntertxContext (TXOption);
}
Private void EntertxContext (TransactionOption TXOption)
{
ServiceConfig CONFIG = New ServiceConfig ();
CONFIG.TRANSACTION = TXOption;
ServiceDomain.enter (config);
}
// by default, the scope is inconsistent;
// to commit the transaction on exit, the consistent flag
// must be set to true before dispose is called
PRIVATE BOOL CONSISTENT = FALSE;
}
System.EnterpriseServices.ServiceDomain Is Available Only On XP SP2 (or Higher) And Windows Server 2003 and Only IN .NET 1.1.
If you need your app to work with .NET 1.0 or on window window 2000 or xp pre-sp2, you can use the trick this don box posted at http://www.gotdotnet.com/team/dbox/default.aspx?key = 2004-07-12T08: 40: 44Z IT Uses Exactly One Transactional ServicComponent Based Class and A Docallback Method to which you pass the delegate to your my mytxcode function That Needs to Execute In a Transaction.