3

I want to use MVC 5 Identity for membership. And I have to use ADO.Net with TransactionScope for all other database transactions. In a method, I need to use Identity and TransactionScope together. But if I use TransactionScope, Create() method throw a timeout expired exception in about 10 seconds. Does anyone know why? Here is content of mentioned method;

public void CreateUser(string password) {
    var userManager = new MyUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));

    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TimeSpan(0, 30, 0)))
    {
        var company = new Company() {
            Id = Guid.NewGuid(),
            Name = "Cmp1"
        };

        AddCompany(company);

        var user = new ApplicationUser()
        {
            UserName = "Usr1",
            CompanyId = company.Id
            //... Other Properties ...
        };

        userManager.Create(user, password);  // The exception is thrown right there.
        scope.Complete();
    }
}

public void AddCompany(Company company)
{
    using (SqlConnection connection = new SqlConnection(CONN))
    {
        SqlCommand command = new SqlCommand();

        command.Connection = connection;
        command.CommandText = "INSERT INTO Companies values(@Id, @Name)";

        command.Parameters.AddWithValue("@Name", company.Name);
        command.Parameters.AddWithValue("@Id", company.Id);

        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
    }
}

The exception.ToString() output is;

System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception: The wait operation timed out
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal(IAsyncResult asyncResult)
   at System.Data.SqlClient.SqlCommand.EndExecuteNonQueryAsync(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
   at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.<ExecuteAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.<UpdateAsync>d__0.MoveNext()
   --- End of inner exception stack trace ---
   at System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.<UpdateAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
   at System.Data.Entity.Core.Objects.ObjectContext.<ExecuteInTransactionAsync>d__3d`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
   at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStoreAsync>d__39.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<ExecuteAsyncImplementation>d__9`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
   at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesInternalAsync>d__31.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNet.Identity.TaskExtensions.CultureAwaiter`1.GetResult()
   at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.<SaveChanges>d__33.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Microsoft.AspNet.Identity.TaskExtensions.CultureAwaiter.GetResult()
   at Microsoft.AspNet.Identity.EntityFramework.UserStore`6.<CreateAsync>d__c.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Microsoft.AspNet.Identity.TaskExtensions.CultureAwaiter.GetResult()
   at Microsoft.AspNet.Identity.UserManager`2.<CreateAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNet.Identity.TaskExtensions.CultureAwaiter`1.GetResult()
   at Microsoft.AspNet.Identity.UserManager`2.<CreateAsync>d__d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Microsoft.AspNet.Identity.AsyncHelper.RunSync[TResult](Func`1 func)
   at Microsoft.AspNet.Identity.UserManagerExtensions.Create[TUser,TKey](UserManager`2 manager, TUser user, String password)
   at Membership.CreateUser(string password) in d:\Code\ProjectName\Membership.cs:line 114
OptimizedQuery
  • 1,262
  • 11
  • 21
madjack
  • 2,321
  • 2
  • 15
  • 22
  • 1
    EF by default opens one connection per request which is terrible under a transaction. Maybe you are running into DTC issues. Post the exception ToString() output. – usr Apr 22 '15 at 11:34
  • What does `AddCompany` do exactly? – DavidG Apr 22 '15 at 11:36
  • Added exception output @usr – madjack Apr 22 '15 at 13:11
  • Added `AddCompany()` @DavidG – madjack Apr 22 '15 at 13:11
  • 1
    I think there are two connections that deadlock. The deadlock is distributed. Therefore SQL Server can't detect and kill it. Ensure that ASP.NET Identity uses the same connection that you are using. If that is not possible you are out of reasonable options. – usr Apr 22 '15 at 16:26
  • I'm sure it use the same connection and I also think that there are two connections but I can not find where the second conection. I think I have to use only EF with Identity. Thanks @usr – madjack Apr 22 '15 at 20:52
  • 3
    I know it has been more than a month since the last post,but... On [this question](http://stackoverflow.com/questions/19385043/what-is-the-recommended-way-to-run-asp-net-identity-functions-in-transaction) and [this answer](http://stackoverflow.com/questions/12724529/how-to-dispose-transactionscope-in-cancelable-async-await/17527843#17527843) is being said that in .NET 4.5 `TransactionScope` was not designed to work with async/await methods and `UserManager` class works with it. I've tried my solution with .NET 4.5.2 and `TransactionScope(TransactionScopeAsyncFlowOption.Enabled)` and it worked. – Nikola Dragićević Jun 03 '15 at 13:18

1 Answers1

-3

This exception comes because of unlimited db calls ....or else please use the transaction scope inside try catch function .....there u can find the error easily

Guanxi
  • 3,103
  • 21
  • 38
Deepan Raj
  • 194
  • 3
  • 11