Just wondering what other’s thoughts are on using a TransactionScope in an IHttpModule.
For example:
BeginRequest
//start new TransactionScope
// start UOW
// Begin UOW transaction
// do some stuff...
EndRequest:
// commit UOW
// commit transaction scope
Is it really a wise idea to hold the transactionscope open for the life of a http request?
I need to write to transactional MSMQ at the same time and have the need for the TransactionScope. I’d like db updates to be persisted as well as MSMQ messages to be sent, or else, roll it all back…
Any advice?
This would fail randomly because in ASP.NET there is no guarantee that BeginRequest and EndRequest will occur on the same thread (aka thread-agility). A Transaction scope will throw an exception if you try to dispose of it on a different thread than the one where it was created. So in the occasional event that EndRequest executed on a different thread then BeginRequest, this exception was thrown.
For more detail:
http://www.mattwrock.com/post/2010/12/26/Getting-TransactionScope-to-play-nice-with-NHibernate.aspx