Alternatives to using Transactional NTFS

2019-02-17 17:53发布

问题:

Given that Microsoft has deprecated Transactional NTFS (TxF):

Microsoft strongly recommends developers utilize alternative means to achieve your application’s needs. Many scenarios that TxF was developed for can be achieved through simpler and more readily available techniques. Furthermore, TxF may not be available in future versions of Microsoft Windows.

While TxF is a powerful set of APIs, there has been extremely limited developer interest in this API platform since Windows Vista primarily due to its complexity and various nuances which developers need to consider as part of application development. As a result, Microsoft is considering deprecating TxF APIs in a future version of Windows to focus development and maintenance efforts on other features and APIs which have more value to a larger majority of customers.

This means that i need an alternative to:

  • CreateTransaction
  • MoveFileTransacted
  • CommitTransaction

My transacted requirements are fairly simple - move two files:

tx = BeginTransaction();
{
   MoveFile(testResults, testResultsArchive); //throws if there's a problem
   MoveFile(cdcResponse, cdcResponseArchive); //throws if there's a problem

   CommitTransaction(tx);
}
finally
{
    CloseHandle(tx);
}

i've thought about turning MoveFile in to CopyFile + DeleteFile:

CopyFile(testResults, testResultsArchive); //throws if there's a problem
CopyFile(cdcResponse, cdcResponseArchive); //throws if there's a problem

DeleteFile(testResults);
DeleteFile(cdcResponse);

But i was hoping for a good solution, not a buggy solution. So i try again:

CopyFile(testResults, testResultsArchive); //throws if there's a problem
CopyFile(cdcResponse, cdcResponseArchive); //throws if there's a problem

try
{
    DeleteFile(testResults);
}
catch (Exception e)
{
   DeleteFile(testResultsArchive);
   throw e;
}
try
{
    DeleteFile(cdcResponse);
}
catch (Exception e)
{
   DeleteFile(cdcResponseArchive);
}

Except i was hoping for a good solution, not a buggy one.

回答1:

Give a try to .NET Transactional File Manager. It is fairly simple to use it safely. The following example from the page shows the way. It even looks like the author is responsive and is able to extend the library with new useful features.

// Wrap a file copy and a database insert in the same transaction
TxFileManager fileMgr = new TxFileManager();
using (TransactionScope scope1 = new TransactionScope())
{
    // Copy a file
    fileMgr.Copy(srcFileName, destFileName);

    // Insert a database record
    dbMgr.ExecuteNonQuery(insertSql);

    scope1.Complete();
} 

In case you are interested in your own transactional manager, be sure you check out this article. If you carefully examine the above mentioned library, you will find that it is implemented right this way.



回答2:

From the link:

As a result, Microsoft is considering deprecating TxF APIs

It isn't dead yet! I don't know why they would remove an atomic file system API for Windows, even if it isn't largely supported yet. There should be a .NET BCL for ease of use leveraging TxF for starters. TxF style support for network copies would also be awesome.

If anything, Microsoft should be improving the API!