Now a days we are using System.Transactions.TransactionScope object for handle transaction. But transaction scope current limitation is that it can not work with filesystem. scenario like I need to
i) Add a record to database
ii) Delete a file from FileSystem
iii) Need to write Xml File
That needs to work as a unit (batch). If one task failed then while task need to failed. If we carefully see that in the tasklist we see filedelete, xmlupdate type complex things is there which is not easy to roolback and we can not use Transaction scope as a tool to handle this scenario due to lacking of file system handle. But I need to use TransactionScope in this scenario because we all know how to use Transactionscope and it is very easy. But how
System.Transactions.IEnlistmentNotification interface come to save life. It is actually commit notification callback for dotnet transaction manager. It has 4 members
1) Void Commit(Enlistment)
2) Void InDoubt(Enlishment)
3) Void Prepare(PreparingEnlishment)
4) Void Rollback(Enlishment)
Detail explanation is in msdn referce. We can see there.
Now see the code sample that use this
We create a TxFileManager which implement IEnlistmentNotification and use it from transactionscope. Actually register it
var obj = new TxFileManager();
For notification purpose. In TxFile Constructor which files we need to store last state is captured and when rollback is required that time we replace those from that place(actually it is manual logic which I executed by the transactionscope object). That way we can use TransactionScope and overcome its limitation.
NB. File state back algorithm might be difference based on senario, performace or some other requirement, But main thing is that it need to handle manually for windows 2003 server. I see on net that in future microsoft implement IEnlistmentNotification interface for its file system object. So I guess in futre TransactionSope can handle it automatically. Means we need to write thouse types of code for future :)