StackOverflow exception when overriding a Task returning method when adding async

别来无恙 提交于 2020-02-05 09:14:08

问题


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.

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;
    }
 }

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 
}

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.

The user 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>();
}

Exception details:

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

UPDATE:

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


回答1:


For completeness, I am posting the working code.

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;
        }

Thanks @HenkHolterman!



来源:https://stackoverflow.com/questions/50316056/stackoverflow-exception-when-overriding-a-task-returning-method-when-adding-asyn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!