Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Creating a new user at background with Hangfire integration fails #6363


User avatar
0
defacto created

We want to create a new user with the help of a backround task by using Hangfire integraion of ABP for 16.000 Employees which is coming from our SAP service. Below is the code which demonstrates for a single creating action. We even couldn't create a single user. There is our code and exception. Please help us.

 public class SyncCreationUserJob : BackgroundJob<SyncCreationUserArgs>, ITransientDependency
    {
        private readonly IUserAppService _userAppService;
        private readonly UserManager _userManager;

        public SyncCreationUserJob(
                    IUserAppService userAppService,
                    UserManager userManager
            )
        {
            _userAppService = userAppService;
            _userManager = userManager;
        }

        public override void Execute(SyncCreationUserArgs args)
        {
            using (_userManager.AbpSession.Use(null, 1))
            {
                var createUserTask = _userAppService.CreateOrUpdateUser(new CreateOrUpdateUserInput
                {
                    User = new UserEditDto
                    {
                        UserName = "TestUser",
                        EmailAddress = "[email protected]",
                        Name = "John",
                        Surname = "Nash",
                        PhoneNumber = "5441111111",
                        IsActive = true,
                        IsLockoutEnabled = false,
                        IsTwoFactorEnabled = false,
                        ShouldChangePasswordOnNextLogin = false,
                        Password = Guid.NewGuid().ToString().Substring(0, 30)
                    },
                    AssignedRoleNames = new string[] { }
                });

                createUserTask.ContinueWith(t =>
                        {
                            var ex = t.Exception;
                        }, TaskContinuationOptions.OnlyOnFaulted);
            }
        }

Message: One or more errors occurred. (Cannot access a disposed object. Object name: 'UserManagerProxy'.)

Stack Trace: at Microsoft.AspNetCore.Identity.UserManager1.ThrowIfDisposed() at Microsoft.AspNetCore.Identity.UserManager1.CreateAsync(TUser user) at Abp.Authorization.Users.AbpUserManager2.CreateAsync(TUser user) in D:\Github\aspnetboilerplate\src\Abp.ZeroCore\Authorization\Users\AbpUserManager.cs:line 130 at Apollo.Authorization.Users.UserAppService.CreateUserAsync(CreateOrUpdateUserInput input) in C:\Yazilim\Apollo\dev\aspnet-core\src\Apollo.Application\Authorization\Users\UserAppService.cs:line 338 at Abp.Threading.InternalAsyncHelper.AwaitTaskWithPostActionAndFinally(Task actualReturnValue, Func1 postAction, Action1 finalAction) in D:\Github\aspnetboilerplate\src\Abp\Threading\InternalAsyncHelper.cs:line 35 at Apollo.Authorization.Users.UserAppService.CreateOrUpdateUser(CreateOrUpdateUserInput input) in C:\Yazilim\Apollo\dev\aspnet-core\src\Apollo.Application\Authorization\Users\UserAppService.cs:line 237 at Abp.Threading.InternalAsyncHelper.AwaitTaskWithPostActionAndFinally(Task actualReturnValue, Func1 postAction, Action1 finalAction) in D:\Github\aspnetboilerplate\src\Abp\Threading\InternalAsyncHelper.cs:line 35 at Abp.Threading.InternalAsyncHelper.AwaitTaskWithFinally(Task actualReturnValue, Action1 finalAction) in D:\Github\aspnetboilerplate\src\Abp\Threading\InternalAsyncHelper.cs:line 15


2 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    CreateOrUpdateUser is an async task. Do:

    AsyncHelper.RunSync(() => _userAppService.CreateOrUpdateUser(...));
    
  • User Avatar
    0
    defacto created

    Thank you for your fast reply!