.net core IsInRoleAsync'System.ObjectDisposedException'

时间:2017-03-12 23:25:10

标签: c# asp.net-core-mvc .net-core-1.1

我使用.net Core 1.1.1进行应用程序 我需要检查当前用户是否是标记帮助程序中管理员角色的成员。 TagHelper的构造函数是

public MyTagHelper(UserManager<User> UserManager, IActionContextAccessor ActionContextAccessor)
    {
        userManager = UserManager;
        actionContextAccessor = ActionContextAccessor;            
    }

然后覆盖处理方法:

 public override async void Process(TagHelperContext context, TagHelperOutput output)
    {
     currentUser = await userManager.GetUserAsync(actionContextAccessor.ActionContext.HttpContext.User);
        isAdmin = await userManager.IsInRoleAsync(currentUser, "admin");
    }

如果保留字符串isAdmin = await userManager.IsInRoleAsync(currentUser, "admin")未注释我有异常:“System.Private.CoreLib.ni.dll中发生了'System.ObjectDisposedException'类型的未处理异常”

我无法理解为什么。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

进程是一种同步方法,并使其成为异步无效意味着它不会等待您的函数完成。您应该改为覆盖ProcessAsync并返回任务。试试这个:

public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
    currentUser = await userManager.GetUserAsync(actionContextAccessor.ActionContext.HttpContext.User);
    isAdmin = await userManager.IsInRoleAsync(currentUser, "admin");
}