Search code examples
aspnetboilerplate

Extending UserManager results in ObjectDisposedException


If I extend the generated aspnetboilerplate UserManager class (from the startup template), any time I use the RoleManager I receive a ObjectDisposedException exception. I assume this is related to dependency injection. Is there something obvious I am doing wrong (or should not be doing)?

Here is the UserManager class definition (with the method I added at the bottom)

namespace MyProject.Authorization.Users
{
    public class UserManager : AbpUserManager<Role, User>
    {
        public UserManager(
            RoleManager roleManager,
            UserStore store, 
            IOptions<IdentityOptions> optionsAccessor, 
            IPasswordHasher<User> passwordHasher, 
            IEnumerable<IUserValidator<User>> userValidators, 
            IEnumerable<IPasswordValidator<User>> passwordValidators,
            ILookupNormalizer keyNormalizer, 
            IdentityErrorDescriber errors, 
            IServiceProvider services, 
            ILogger<UserManager<User>> logger, 
            IPermissionManager permissionManager, 
            IUnitOfWorkManager unitOfWorkManager, 
            ICacheManager cacheManager, 
            IRepository<OrganizationUnit, long> organizationUnitRepository, 
            IRepository<UserOrganizationUnit, long> userOrganizationUnitRepository, 
            IOrganizationUnitSettings organizationUnitSettings, 
            ISettingManager settingManager)
            : base(
                roleManager, 
                store, 
                optionsAccessor, 
                passwordHasher, 
                userValidators, 
                passwordValidators, 
                keyNormalizer, 
                errors, 
                services, 
                logger, 
                permissionManager, 
                unitOfWorkManager, 
                cacheManager,
                organizationUnitRepository, 
                userOrganizationUnitRepository, 
                organizationUnitSettings, 
                settingManager)
        {
        }

        public void GoBoom()
        {
            var role = RoleManager.Roles.FirstOrDefault();
        }
    }
}

Usage (here, directly injected and used on a razor view)

@inject MyProject.Authorization.Users.UserManager _userManager
@{
    _userManager.GoBoom();
}

Solution

  • Add [UnitOfWork] attribute and make it a virtual method:

    [UnitOfWork]
    public virtual void GoBoom()
    {
        ...
    }
    

    See: UnitOfWork Attribute Restrictions

    You can use UnitOfWork attribute for:

    • All public or public virtual methods for classes that are used over an interface (Like an application service used over a service interface).
    • All public virtual methods for self-injected classes (Like MVC Controllers and Web API Controllers).
    • All protected virtual methods.