asp.net-coreblazorasp.net-identityblazor-server-side

UserManager.GetClaimsAsync() failing - The connection is closed


In a Blazor InteractiveServer application, using the ASP.NET Identity Library, I call: Microsoft.AspNetCore.Identity.UserManager`1.GetClaimsAsync(TUser user)

I set it to use my extension class by adding it in Program.cs

builder.Services.AddScoped<AuthenticationStateProvider, ExAuthenticationStateProvider>();

Other than adding that line, all of the use of this is the Blazor stack calling this when it chooses to. The only time I directly call anything in this class is when I call ResetNextCheck().

And it throws (full stack trace below).

Microsoft.EntityFrameworkCore.Query - An exception occurred while iterating over the results of a query for context type 'LouisHowe.web.Areas.Identity.Data.UserDbContext'.
System.InvalidOperationException: Invalid operation. The connection is closed.

The claims table has 34 entries so reading the entire table, if requested (it isn't) is fast.

The one non-standard part of all this in my app is I have class ExIdentityUser : IdentityUser where I add a public bool Enabled { get; set; }. And so my DbContext is UserDbContext : IdentityDbContext<ExIdentityUser>.

This is occurring inside my ExAuthenticationStateProvider : ServerAuthenticationStateProvider GetAuthenticationStateAsync() method.

Why is this closing the connection in this call?

Full log:

Error 08:52:21 [] Microsoft.EntityFrameworkCore.Database.Command - Failed executing DbCommand (19ms) [Parameters=[@__user_Id_0='?' (Size = 450)], CommandType='Text', CommandTimeout='30']
SELECT [a].[Id], [a].[ClaimType], [a].[ClaimValue], [a].[UserId]
FROM [AspNetUserClaims] AS [a]
WHERE [a].[UserId] = @__user_Id_0

// 20 irrelevant log messages

Error 08:52:22 [] Microsoft.EntityFrameworkCore.Query - An exception occurred while iterating over the results of a query for context type 'LouisHowe.web.Areas.Identity.Data.UserDbContext'.
System.InvalidOperationException: Invalid operation. The connection is closed.
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync() InvalidOperationException: Invalid operation. The connection is closed.
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
Error 08:52:22 [] LouisHowe.web.Services.ExAuthenticationStateProvider - GetAuthenticationStateAsync() exception: Invalid operation. The connection is closed. InvalidOperationException: Invalid operation. The connection is closed.
   at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.InitializeReaderAsync(AsyncEnumerator enumerator, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserOnlyStore`6.GetClaimsAsync(TUser user, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Identity.UserManager`1.GetClaimsAsync(TUser user)
   at LouisHowe.web.Services.ExAuthenticationStateProvider.GetAuthenticationStateAsync() in D:\a\LouisHowe\LouisHowe\LouisHowe.web\Services\ExAuthenticationStateProvider.cs:line 94

Solution

  • I fixed it (explanation below). With that said, I don't know why this fixes it.

    I was using UserManager<ExIdentityUser> to call:

    userManager.FindByIdAsync(id);
    userManager.GetClaimsAsync(user);
    

    Those call were occasionally failing because the underlying DbConnection was closed. I have no idea why, especially as sometimes the call to FindByIdAsync() would succeed, then the immediately following call to GetClaimsAsync() would have a closed connection.

    To work around this I changed it to use:

    IDbContextFactory<UserDbContext> UserDbFactory;
    var dbContext = await UserDbFactory.CreateDbContextAsync();
    var user = await dbContext.Users.Where(u => u.Id == id).FirstOrDefaultAsync();
    var list = await dbContext.UserClaims.Where(uc => uc.UserId == id).ToListAsync();
    

    That not only resolved my issue with the DbConnection sometimes being closed, it also resolved this issue. As I said above, no idea why. But calling the UserManager methods did something to set NotifyAuthenticationStateChanged() to fail. Switching to UserDbFactory fixed this issue as well as the other issue.