The transaction has aborted. Tricky .NET TransactionScope Behavior.

This Error took me a bunch of time to find. Why? Well, I’m just not smart enough.

The code says everything:

public void When_some_transaction_disposes_without_error_or_complete()
    // entering an ambient transaction
    new TransactionScope();

    // start a nested transaction
    var t2 = new TransactionScope();
    // no complete, no exception
    // cancels ambient transaction, too

    // creating a new scope fails
    Action createAScope = () => new TransactionScope();
            .Message.ShouldEqual("The transaction has aborted.");

In my case, t2 was opened somewhere else only when some conditions were given. To me, the exception seemed to occur randomly.

For sure, after finding the cause the message starts to make sense too. But in the beginning I couldn’t figure out what the error was about.

Hope this post helps someone who consults Google for “TransactionAbortedException” or “The transaction has aborted.”