鉴于Microsoft已弃用Transactional NTFS(TxF):
Microsoft强烈建议开发人员使用其他方法来满足您的应用程序需求.可以通过更简单和更容易获得的技术来实现TxF开发的许多场景.此外,在未来的Microsoft Windows版本中可能无法使用TxF.
虽然TxF是一组功能强大的API,但自Windows Vista以来,开发人员对此API平台的兴趣非常有限,主要是由于其复杂性和开发人员在应用程序开发过程中需要考虑的各种细微差别.因此,Microsoft正在考虑在未来版本的Windows中弃用TxF API,以便将开发和维护工作集中在对大多数客户更有价值的其他功能和API上.
这意味着我需要一个替代方案:
我的交易要求相当简单 - 移动两个文件:
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);
}
Run Code Online (Sandbox Code Playgroud)
我已经考虑过转向MoveFile
到CopyFile
+ DeleteFile
:
CopyFile(testResults, testResultsArchive); //throws if there's a problem
CopyFile(cdcResponse, cdcResponseArchive); //throws if there's a problem
DeleteFile(testResults);
DeleteFile(cdcResponse);
Run Code Online (Sandbox Code Playgroud)
但我希望有一个好的解决方案,而不是一个错误的解决方案.所以我再试一次:
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);
}
Run Code Online (Sandbox Code Playgroud)
除了我希望有一个好的解决方案,而不是一个有缺陷的解决方案.
从链接:
因此,Microsoft 正在考虑弃用 TxF API
还没死呢!我不知道为什么他们会删除 Windows 的原子文件系统 API,即使它还没有得到广泛支持。应该有一个 .NET BCL 以便于使用 TxF 作为初学者。对网络副本的 TxF 样式支持也很棒。
如果有的话,微软应该改进 API!
尝试一下.NET 事务文件管理器。安全使用它相当简单。该页面的以下示例显示了该方法。甚至看起来作者反应灵敏,并且能够使用新的有用功能扩展该库。
// 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();
}
Run Code Online (Sandbox Code Playgroud)
如果您对自己的交易经理感兴趣,请务必查看这篇文章。如果你仔细检查上面提到的库,你会发现它就是这样实现的。
归档时间: |
|
查看次数: |
1796 次 |
最近记录: |