linq2db / LinqToDB.Identity

ASP.NET Core Identity provider that uses LinqToDB.
https://linq2db.github.io/
MIT License
46 stars 9 forks source link

Transaction scope not working? #17

Closed DmitrijOkeanij closed 5 years ago

DmitrijOkeanij commented 5 years ago

I have this code

 using (var transaction = new TransactionScope())
 {
     IdentityResult result = await userManager.CreateAsync(user, model.Password);

Execution goes inside userManager.CreateAsync, and freeze. No exception, no result.

MaceWindu commented 5 years ago

Do you have stack to determine where it waits?

DmitrijOkeanij commented 5 years ago

Here is my code

using (var transaction = new TransactionScope())
{
    IdentityResult result = await userManager.CreateAsync(user, model.Password);
    await db.Users.Where(x => x.Id == user.Id).Set(x => x.Link, x => x.Id.ToString()).UpdateAsync();

I tried to debug but any times it breakes in different points. Sometimes it goes to await db.Users.Where(x => x.Id == user.Id).Set(x => x.Link, x => x.Id.ToString()).UpdateAsync(); Sometimes brokes inside IdentityResult result = await userManager.CreateAsync(user, model.Password); in different places inside it.

I am not understanding it.

I think this is bug of multythreads debugging?

I use Rider for debug and Ubuntu 18.10 to run.

DmitrijOkeanij commented 5 years ago

I have only this

System.InvalidOperationException: A TransactionScope must be disposed on the same thread that it was created. at System.Transactions.TransactionScope.Dispose() at My.Controllers.AuthController.Register(NewUserViewModel model) in /path/Controllers/AuthController.cs:line 139 at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)

MaceWindu commented 5 years ago

Looks like TransactionScope is not designed for async execution. Microsoft added TransactionScopeAsyncFlowOption.Enabled to handle it, so you should try it first

https://weblogs.thinktecture.com/pawel/2018/06/entity-framework-core-use-transactionscope-with-caution.html

DmitrijOkeanij commented 5 years ago

All works fine. Thank you! Maybe to add this to doc?

MaceWindu commented 5 years ago

Will do