Transactions with Windows Vista and Windows Server 2008 C# Help

You can write a custom durable resource manager that works with the File and Registry classes. A filebased
durable resource manager can copy the original file and write changes to the temporary file inside a temporary directory to make the changes persistent. When committing the transaction, the original file is replaced by the temporary file. Writing custom durable resource managers for files and the registry is no longer necessary with Windows Vista and Windows Server 2008. With these operating systerps, native transactions with the file system and with the registry are supported. For this, there are new API calls  uch aSCreateFileTransacted(),CreateHardLinkTransacted(),CreateSymbolicLinkTransacted(), CopyFile’:’ransacted (), and so on. What these API calls have in common is that they require a handle to a transaction passed as an argument; they do not support ambient transactions. The transactional API calls are not available from .NET 3.5, but you can create a custom wrapper by using Pla t form Invoke.

Platfo=-:n Invoke is discussed in mOTedetail ill Chapter 24, “Tnteroperabilitg.”

The sample application wraps the native method CreateFileTransacted () for creating transactional”
file streams from .NET applications.

When invoking native methods, the parameters of the native methods must be mapped to .NET data types. Because of security issues, .NET 2.0 introduced the class SafeHandle to map a native HANDLE type. SafeHand:’e is an abstract type that wraps operating system handles and supports critical finalization of handle resources. Depending on the allowed values of a handle, the derived classes SafeHandleMinusOneIsInvalid and SafeHandleZeroOrMinusOneIsInvalid can be used to wrap native handles. SafeFileHandle itself derives from SafeHandleZeroOrMinusOneIsInvalid. To map a handle to a transaction, the class SafeTransactionHandle is defined.


All native methods used from .NET are defined with the classNati veMethods shown here.With the sample, the nativeAPls needed are CreateFileTransacted () and CloseHandle (},which are defined as staticmembers of theclass.The methods are declared extern because thereisno C# implementation. Instead,the implementation isfound in the native DLL as defined by the attributeDlllmport. Both of thesemethods can be found in the nativeDLL Kerne132. dll. With the method declaration,the parameters defined with the Windows API callare mapped to .NET data types.The parameter tXHandle representsa handle toa transactionand isof the previously defined type SafeTransactionHandle.

Now itisvery easy to use the transactionalAPI from .NET code. You can createan ambient transaction with the TransactionScope classand use the TransactedFile classwithin the contextof th«:..ambient

transaction scope. If the transaction is aborted, the file is not written If Utetransaction is committed, you can find the file in the temp directory..

Now you can use databases, volatile resources, and files within the same transaction…

Posted on October 31, 2015 in Transactions

Share the Story

Back to Top