Joint WebSecurity.CreateUser and DbContext operations escalate to DTC

278 views Asked by At

Recently I ran into a glitch, while trying to wrap up some operations involing both WebSecurity and DbContext with a transaction.

In my Asp.net MVC 4 app, I'm trying to register a new user this way:

var transactionOptions = new TransactionOptions
{
    IsolationLevel = IsolationLevel.ReadCommitted,
    Timeout = TransactionManager.MaximumTimeout
};
using (var transactionScope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
{
    var confirmationToken = WebSecurity.CreateUserAndAccount(
    vm.Email,
    vm.Password,
    new
    {
        AccountCreationDate = DateTime.Now,
        City = vm.City,
        (...)
    },
    true);

    Roles.AddUserToRole(vm.Email, "Client");

    // Some other job on DbContext in Repository
    var userRepository = new UserRepository();
    userRepository.AssignData(vm.Email);

    transactionScope.Complete();
}

As you can see, I'm trying to create both an user account and membership entry, assign a role and then do some additional stuff on EF5 DbContext via repository.

This code is wrapped within a try-catch block, which I don't include here for brevity.

And here comes my question: is it possible to wrap all this 3 hits to the Database (respectively through WebSecurity, Roles and Repository) with one transaction WITHOUT letting it to escalate to DTC?

I'm using SQL Server 2012, one DB and the same connection string if it helps.

0

There are 0 answers