3

Je souhaite utiliser MVC 5 Identity pour l'appartenance. Et je dois utiliser ADO.Net avec TransactionScope pour toutes les autres transactions de base de données. Dans une méthode, j'ai besoin d'utiliser Identity et TransactionScope ensemble. Mais si j'utilise TransactionScope, Create() méthode jeter une exception de délai d'expiration expiré en environ 10 secondes. Quelqu'un sait-il pourquoi? Voici le contenu de la méthode mentionnée;MVC 5 Exception de délai d'attente d'autorisation d'utilisation lors de l'utilisation avec TransactionScope

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(); 
    } 
} 

La sortie exception.ToString() est;

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 
+1

EF par défaut ouvre un connexion par demande ce qui est terrible dans le cadre d'une transaction. Peut-être que vous rencontrez des problèmes de DTC. Publiez l'exception ToString() en sortie. – usr

+0

Que fait exactement 'AddCompany'? – DavidG

+0

Sortie d'exception ajoutée @usr – madjack

Répondre

-1

Cette exception vient du fait que des appels illimités db .... ou bien s'il vous plaît utiliser la portée de la transaction dans la fonction de capture d'essayer ..... il y a u peut trouver l'erreur facilement