Independent transactions in a post-TransactionScope world

Topics: Customizing Orchard, Troubleshooting, Writing modules
May 12, 2014 at 5:12 PM
For reasons outside my control (and even that of the client) I need to be able to store certain information in the database even if the transaction for the current request gets rolled back. It used to be possible, from what I've read, via the TransactionScope class, but since it's been removed there doesn't seem to be any clean, or even dirty way of doing this.

I've tried resolving a SessionLocator and creating a Repository manually, but that still ended up putting the record I'm trying to store in the same transaction as everything else. I also tried getting a IWorkContextAccessor and use it to create a new context for resolving a fresh Repository, but that caused my theme to break and still didn't lead to the record being created in the database.

Any ideas of how to handle this in Orchard? Or do I need to add all my own DB code and skip what Orchard provides for database access?
May 12, 2014 at 6:47 PM
May 13, 2014 at 1:43 PM
Given that that SO question is about dependency injection, I assume there's no way to get an out of band transaction then? Because that doesn't seem to answer my question. I already have the table I need to access set up through Orchard, I just have to be able to make changes to the data in it without risking that the current request's default transaction will roll it back.
May 13, 2014 at 11:30 PM
Edited May 27, 2014 at 9:04 AM
You can do this like:
  1. Create a new life time scope: _lifetimeScope.BeginLifetimeScope() (injected ILifetimeScope).
  2. scope.Resolve<ITransactionManager>().RequireNew();
  3. scope.Resolve() then e.g. an IRepository, but probably will only work with ISessionLocator.
May 14, 2014 at 2:20 PM
Thanks, Piedone, that works! Well, I do need to follow up with another scope.Resolve<ITransactionManager>().RequireNew(); when I'm done adding the new rows, but apart from that your instructions did the trick.
May 27, 2014 at 9:04 AM
Edited my answer: You can inject ILifetimeScope directly.