添加异步时覆盖Task返回方法时出现StackOverflow异常 [英] StackOverflow exception when overriding a Task returning method when adding async

查看:168
本文介绍了添加异步时覆盖Task返回方法时出现StackOverflow异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的用户管理器类(ASP .NET Identity).我想覆盖FindByIdMethod,以便它自动加载用户模型的角色名称.这是我要覆盖的唯一方法.我正在使用Microsoft.Identity nuget软件包2.2.1,Asp.Net Framework.

I have a custom user manager class (ASP .NET Identity). I want to override the FindByIdMethod, so that it automatically loads the role names for the user model. This is the only method I am overriding. I am using Microsoft.Identity nuget packages version 2.2.1, Asp.Net Framework.

但是,下面的代码将引发StackOverflow异常-在await base.FindByIdAsync(userId);

However, the code below throws a StackOverflow exception - at await base.FindByIdAsync(userId);

 public class MyUserManager : UserManager<MyUser>, IMyUserManager
 {
    public override async Task<MyUser> FindByIdAsync(string userId)
    {
        var user = await base.FindByIdAsync(userId);
        user.RoleNames = await this.GetRolesAsync(user.Id);
        return user;
    }
 }

当我尝试类似的代码而不向覆盖的签名添加'async'时,它可以正常工作-但是,我无法在此方法中加载角色名称:

When I try similar code without adding 'async' to the overriden signature it works OK - however, I cannot load my role names in this method:

public override Task<MyUser> FindByIdAsync(string userId)
{
    var userTask = base.FindByIdAsync(userId); //no errors in this approach
    return userTask; //but also no custom behaviour 
}

我怀疑IMyUserManager可能有问题,但是IOC/DI需要它.这是一个自动生成的界面,因此具有UserManager类的所有公共成员.

I suspect maybe the IMyUserManager might be problematic, but I need it for the IOC/DI. This was an autogenerated interface, so it has all the public members of the UserManager class.

用户类别:

public class MyUser: IdentityUser
{
    public string DisplayName { get; set; }
    public bool IsActivated { get; set; }
    public bool MustChangePassword { get; set; }
    public IList<string> RoleNames { get; set; } = new List<string>();
}

异常详细信息:

调用堆栈(TelimenaUserManager = MyUserManager,我简化了名称)

Call stack (TelimenaUserManager = MyUserManager, I simplified the name)

更新:

感谢Henk在评论中的建议,我看到递归发生在GetRolesAsync方法中...

Thanks to Henk's suggestions in the comments, I see that recursion happens in the GetRolesAsync method...

推荐答案

为完整起见,我正在发布工作代码.

For completeness, I am posting the working code.

我必须将当前的"IUserStore"强制转换为"IUserRoleStore"(需要两个通用参数!).

I had to cast the current 'IUserStore' to be the 'IUserRoleStore' (Both generic arguments are needed!).

然后基于用户实例获取角色(这避免了查找用户"的递归操作)

Then get the roles based on the user instance (which avoids the recursion of 'Finding the user')

 public override async Task<MyUser> FindByIdAsync(string userId)
        {
            var user = await base.FindByIdAsync(userId);
            var roleStore = this.Store as IUserRoleStore<MyUser, string>;
            user.RoleNames = await roleStore.GetRolesAsync(user);
            return user;
        }

感谢@HenkHolterman!

Thanks @HenkHolterman!

这篇关于添加异步时覆盖Task返回方法时出现StackOverflow异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆