Microsoft Identity Framework 将角色分配给 Postgres 数据库的用户错误 - CancellationTokenSource 已被处置
Microsoft Identity Framework assigning roles to user error with Postgres Database - The CancellationTokenSource has been disposed
您好,我在为使用 Identity Framework 5.0.5、.net5.0.5 和 Postgres SQL 数据库的用户分配角色时遇到问题。
可以在下面看到用于将角色分配给用户的代码以及下面的错误。
如果可能请帮忙。
非常感谢!
{
if (!await roleManager.RoleExistsAsync("Admin"))
{
await roleManager.CreateAsync(new IdentityRole
{
Name = "Admin"
});
}
ApplicationUser user = await userManager.FindByIdAsync(userId);
if (isAdmin)
{
await userManager.AddToRoleAsync(user, "Admin");
}
else
{
await userManager.RemoveFromRoleAsync(user, "Admin");
}
}
}
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
Configuration.GetConnectionString("DefaultConnection"),
builder => builder.EnableRetryOnFailure(50, TimeSpan.FromSeconds(5), null)));
System.ObjectDisposedException: The CancellationTokenSource has been disposed.
at System.Threading.CancellationTokenSource.ThrowObjectDisposedException()
at System.Threading.CancellationTokenSource.CancelAfter(Int32 millisecondsDelay)
at System.Threading.CancellationTokenSource.CancelAfter(TimeSpan delay)
at Npgsql.Util.ResettableCancellationTokenSource.Stop()
at Npgsql.NpgsqlReadBuffer.<Ensure>g__EnsureLong|40_0(NpgsqlReadBuffer buffer, Int32 count, Boolean async, Boolean readingNotifications)
at Npgsql.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|194_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
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(DbContext _, Boolean result, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at SearchDataScreening.Pages.UserManagement.OnGetAdminControl(String userId, Boolean isAdmin) in /Users/dewaldthattingh/GitHubRepos/TPS/SearchScreening/SearchDataScreening/Pages/UserManagement.cshtml.cs:line 50
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__140_1(Object state)
at System.Threading.QueueUserWorkItemCallback.<>c.<.cctor>b__6_0(QueueUserWorkItemCallback quwi)
at System.Threading.ExecutionContext.RunForThreadPoolUnsafe[TState](ExecutionContext executionContext, Action`1 callback, TState& state)
at System.Threading.QueueUserWorkItemCallback.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
请尝试将其更改为不使用如下所示的 await 方法
bool roleExist = roleManager.RoleExistsAsync("Admin").Result;
if (!roleExist)
{
var x = roleManager.CreateAsync(new IdentityRole
{
Name = "Admin"
}).Result;
}
if (isAdmin)
{
var user = userManager.FindByIdAsync(userId).Result;
if (user != null)
{
var x = userManager.AddToRoleAsync(user, "Admin").Result;
}
}
else
{
var user = userManager.FindByIdAsync(userId).Result;
if (user != null)
{
var x = userManager.RemoveFromRoleAsync(user, "Admin").Result;
}
}
您好,我在为使用 Identity Framework 5.0.5、.net5.0.5 和 Postgres SQL 数据库的用户分配角色时遇到问题。
可以在下面看到用于将角色分配给用户的代码以及下面的错误。
如果可能请帮忙。
非常感谢!
{
if (!await roleManager.RoleExistsAsync("Admin"))
{
await roleManager.CreateAsync(new IdentityRole
{
Name = "Admin"
});
}
ApplicationUser user = await userManager.FindByIdAsync(userId);
if (isAdmin)
{
await userManager.AddToRoleAsync(user, "Admin");
}
else
{
await userManager.RemoveFromRoleAsync(user, "Admin");
}
}
}
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(
Configuration.GetConnectionString("DefaultConnection"),
builder => builder.EnableRetryOnFailure(50, TimeSpan.FromSeconds(5), null)));
System.ObjectDisposedException: The CancellationTokenSource has been disposed.
at System.Threading.CancellationTokenSource.ThrowObjectDisposedException()
at System.Threading.CancellationTokenSource.CancelAfter(Int32 millisecondsDelay)
at System.Threading.CancellationTokenSource.CancelAfter(TimeSpan delay)
at Npgsql.Util.ResettableCancellationTokenSource.Stop()
at Npgsql.NpgsqlReadBuffer.<Ensure>g__EnsureLong|40_0(NpgsqlReadBuffer buffer, Int32 count, Boolean async, Boolean readingNotifications)
at Npgsql.NpgsqlConnector.<ReadMessage>g__ReadMessageLong|194_0(NpgsqlConnector connector, Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, Boolean isReadingPrependedMessage)
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
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(DbContext _, Boolean result, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at SearchDataScreening.Pages.UserManagement.OnGetAdminControl(String userId, Boolean isAdmin) in /Users/dewaldthattingh/GitHubRepos/TPS/SearchScreening/SearchDataScreening/Pages/UserManagement.cshtml.cs:line 50
at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__140_1(Object state)
at System.Threading.QueueUserWorkItemCallback.<>c.<.cctor>b__6_0(QueueUserWorkItemCallback quwi)
at System.Threading.ExecutionContext.RunForThreadPoolUnsafe[TState](ExecutionContext executionContext, Action`1 callback, TState& state)
at System.Threading.QueueUserWorkItemCallback.Execute()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
请尝试将其更改为不使用如下所示的 await 方法
bool roleExist = roleManager.RoleExistsAsync("Admin").Result;
if (!roleExist)
{
var x = roleManager.CreateAsync(new IdentityRole
{
Name = "Admin"
}).Result;
}
if (isAdmin)
{
var user = userManager.FindByIdAsync(userId).Result;
if (user != null)
{
var x = userManager.AddToRoleAsync(user, "Admin").Result;
}
}
else
{
var user = userManager.FindByIdAsync(userId).Result;
if (user != null)
{
var x = userManager.RemoveFromRoleAsync(user, "Admin").Result;
}
}