How to implement DTC in .NET
DTC is implemented using the COM+.
Following are some of the steps to implement COM + in .NET:-
1) The "EnterpriseService" namespace has all the classes by which we can implement the DTC in .NET. You have to add reference "EnterpriseService" namespace.
2) You must derive your class from "Serviced Component" object.
3) After that you have to define your class with the transaction attribute
[ Transaction(TransactionOption.RequiresNew) ]
4) When the class level transaction type is defined. Now its time to define at the method level the AutoComplete attribute. Autocomplete attribute explains that if no exception is thrown then mark its part of the transaction as okay. This helps to cut down on the amount of code required. If the implementation sets AutoComplete to false, or omits it altogether, then we would require to manage the transaction manually. For manually control the transaction you will require to use the ContextUtil class & its static members.
public void SampleFunction()
{
try
{
// Do something to a database
// ...
// Everything okay so far Commit the transaction
ContextUtil.SetComplete();
}
catch(Exception)
{
// Something went wrong Abort and Rollback the Transaction.
ContextUtil.SetAbort();
}
}
5)The Component which is derived from "ServicedComponent" must be strong named as they run under COM+.
6)Once the classes are compiled using the string name.Register the Component in COM+ services using
regsvcs c:\DllPath\TransactionComponent.dll
7)The component is registered by using the COM+ explorer.