Plusce
Plusce

Reputation: 117

ObjectDisposedException in ASP.NET Boilerplate manage services

I've decided to separate logic from RoleAppService to RoleService, because I need to use role methods in other app services. By separating logic I can avoid injecting endpoints to other endpoints (e.g. RoleAppService to UserAppService) and possible circular dependency. API is also not visible outside - that's why class inherit from AbpServiceBase, no ApplicationService (which make service visible in swagger). Unfortunately, when I try to use boilerplate manager classes (e.g. RoleManager, UserManager) inside RoleService method, following exception is thrown:

Cannot access a disposed object.
Object name: 'RoleManagerProxy'.

With stack trace:

   at Microsoft.AspNetCore.Identity.RoleManager`1.ThrowIfDisposed()
   at Microsoft.AspNetCore.Identity.RoleManager`1.FindByNameAsync(String roleName)
   at Castle.Proxies.Invocations.RoleManager`1_FindByNameAsync.InvokeMethodOnTarget()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.RoleManagerProxy.FindByNameAsync(String roleName)
   at Abp.Authorization.Roles.AbpRoleManager`2.<CheckDuplicateRoleNameAsync>d__48.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Abp.Authorization.Roles.AbpRoleManager`2.<UpdateAsync>d__42.MoveNext() in D:\Github\aspnetboilerplate\src\Abp.ZeroCore\Authorization\Roles\AbpRoleManager.cs:line 283
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at VZero.Roles.RoleService.<Create>d__6.MoveNext() in C:\Users\Marcin\source\repos\translink.vendorforum\Translink.Api\src\VZero.Application\Roles\RoleService.cs:line 42
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at VZero.Roles.RoleService.<CreateOtherDocumentUserRole>d__9.MoveNext() in C:\Users\Marcin\source\repos\translink.vendorforum\Translink.Api\src\VZero.Application\Roles\RoleService.cs:line 94
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at VZero.Roles.RoleService.<GetOtherDocumentRole>d__11.MoveNext() in C:\Users\Marcin\source\repos\translink.vendorforum\Translink.Api\src\VZero.Application\Roles\RoleService.cs:line 107
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at VZero.Roles.RoleService.<UpdateOtherDocumentUserRole>d__10.MoveNext() in C:\Users\Marcin\source\repos\translink.vendorforum\Translink.Api\src\VZero.Application\Roles\RoleService.cs:line 100
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at VZero.Users.UserAppService.<Update>d__20.MoveNext() in C:\Users\Marcin\source\repos\translink.vendorforum\Translink.Api\src\VZero.Application\Users\UserAppService.cs:line 226
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Abp.Threading.InternalAsyncHelper.<AwaitTaskWithPostActionAndFinallyAndGetResult>d__5`1.MoveNext() in D:\Github\aspnetboilerplate\src\Abp\Threading\InternalAsyncHelper.cs:line 120
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at lambda_method(Closure , Object )
   at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.AwaitableObjectResultExecutor.<Execute>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeNextActionFilterAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextExceptionFilterAsync>d__24.MoveNext()

Error seems to be related with framework configuration. I've tried to end service name with AppService, but it didn't help at all. Service is registered in Windsor Castle framework. What interesting - inside class properly works boilerplate repositories, like RoleRepository, UserReposistory - unfortunately RoleManager/UserManager not. Below is the fragment of the the class:

public class RoleService : AbpServiceBase, IRoleService
    {
        private readonly RoleManager _roleManager;
        private readonly IRepository<Role> _roleRepository;
        private readonly UserManager _userManager;
        private readonly IRepository<User, long> _userRepository;
        private readonly IPermissionManager _permissionManager;

        public RoleService(RoleManager roleManager, IRepository<Role> roleRepository,
            UserManager userManager, IRepository<User, long> userRepository,
            IPermissionManager permissionManager)
        {
            _roleManager = roleManager;
            _roleRepository = roleRepository;
            _userManager = userManager;
            _userRepository = userRepository;
            _permissionManager = permissionManager;
        }

        public async Task<RoleDto> Create(CreateRoleDto createRoleDto, Action<IdentityResult> checkErrorsMethod)
        {
            var role = ObjectMapper.Map<Role>(createRoleDto);
            role.SetNormalizedName();
            await _roleManager.UpdateAsync(role); // here exception is thrown

            var grantedPermissions = _permissionManager
                .GetAllPermissions()
                .Where(p => createRoleDto.Permissions.Contains(p.Name))
                .ToList();

            await _roleManager.SetGrantedPermissionsAsync(role, grantedPermissions);
            return role.MapTo<RoleDto>();
        }

I would be so grateful for any help.

Upvotes: 0

Views: 700

Answers (1)

bobj
bobj

Reputation: 98

If you add [UnitOfWork] attribute, don't forget to make method public virtual

Upvotes: 2

Related Questions