问题描述
我在调用以下代码块时收到此错误。我在 SO 上看到的与此错误相关的所有内容都是由于人们调用了 using
块,而我不是,但在幕后可能有一些我不知道被复制的代码?我也不确定如何找到这里复制的连接。任何有专业知识的人都可以帮助解决这个问题?我的堆栈跟踪也在下面。
if (await _userManager.IsInRoleAsync(user,role.Name))
public class UserRolesModel : PageModel
{
private readonly UserManager<IdentityUser> _userManager;
private readonly SignInManager<IdentityUser> _signInManager;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly IAdminView _adminView;
public UserRolesModel(UserManager<IdentityUser> userManager,SignInManager<IdentityUser> signInManager,RoleManager<IdentityRole> roleManager,IAdminView adminView)
{
_userManager = userManager;
_signInManager = signInManager;
_roleManager = roleManager;
_adminView = adminView;
}
public EmployeeDto Employee { get; set; }
public ManageUserRolesDto UserRoles { get; set; }
public async Task OnGetAsync(string userId)
{
var userRoleList = new List<UserRolesDto>();
var user = await _userManager.FindByIdAsync(userId);
foreach (var role in _roleManager.Roles)
{
var userRolesDto = new UserRolesDto()
{
RoleName = role.Name
};
if (await _userManager.IsInRoleAsync(user,role.Name))
{
userRolesDto.Selected = true;
}
else
{
userRolesDto.Selected = false;
}
userRoleList.Add(userRolesDto);
}
UserRoles = new ManageUserRolesDto()
{
UserId = userId,UserRoles = userRoleList
};
var employeeData = await _adminView.GetEmployeeDataByAspId(userId);
var viewmodel = new EmployeeViewDto
{
Employee = employeeData
};
Employee = new EmployeeDto
{
EmployeeAspId = employeeData.AspUserId,EmployeeViewDto = viewmodel
};
}
public async Task<IActionResult> OnPostAsync(string id,ManageUserRolesDto userRoles)
{
var user = await _userManager.FindByIdAsync(id);
var roles = await _userManager.GetRolesAsync(user);
var result = await _userManager.RemoveFromrolesAsync(user,roles);
result = await _userManager.AddToRolesAsync(user,userRoles.UserRoles.Where(x => x.Selected).Select(y => y.RoleName));
var currentUser = await _userManager.GetUserAsync(User);
await _signInManager.RefreshSignInAsync(currentUser);
await Data.Seeds.DefaultUsers.SeedSuperAdminAsync(_userManager,_roleManager);
return RedirectToPage("/Admin/Index");
}
}
堆栈跟踪
system.invalidOperationException: There is already an open DataReader associated with this Connection which must be closed first.
at Microsoft.Data.sqlClient.sqlCommand.<>c.<ExecuteDbDataReaderAsync>b__169_0(Task`1 result)
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.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(DbContext _,Boolean result,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.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable,CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable,CancellationToken cancellationToken)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore`9.IsInRoleAsync(TUser user,String normalizedRoleName,CancellationToken cancellationToken)
at Microsoft.AspNetCore.Identity.UserManager`1.IsInRoleAsync(TUser user,String role)
at Navrae.WebApp.Pages.RoleManager.UserRolesModel.OnGetAsync(String userId) in C:\Users\Work Laptop\source\repos\Navrae\Navrae.WebApp\Pages\RoleManager\UserRoles.cshtml.cs:line 45
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.ExecutorFactory.NonGenericTaskHandlerMethod.Execute(Object receiver,Object[] arguments)
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeHandlerMethodAsync()
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeNextPageFilterasync()
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(State& next,Scope& scope,Object& state,Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterasync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker,Task lastTask,State next,Scope scope,Object state,Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next,Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker,Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker,Task task,Idisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint,Task requestTask,ILogger logger)
at Microsoft.AspNetCore.Authorization.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next,HttpContext context,AuthorizationPolicy policy,PolicyAuthorizationResult authorizeResult)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)