IdentityUserManager fails silently when used in module's application service
Added custom properties to AppUser which is in a module project using this https://github.com/abpframework/abp/issues/2698.
now trying to create the AppUser with custom properties using this https://github.com/abpframework/abp/issues/2268#issuecomment-559380876
public class AppUserAppService :
CrudAppService<AppUser, AppUserDto, Guid, PagedAndSortedResultRequestDto,
CreateUpdateAppUserDto, CreateUpdateAppUserDto>,
IAppUserAppService
{
private readonly IServiceProvider serviceProvider;
private readonly IdentityUserManager userManager;
public AppUserAppService(IAppUserRepository repository, IdentityUserManager userManager)
: base(repository)
{
this.userManager = userManager;
}
public override async Task<AppUserDto> CreateAsync(CreateUpdateAppUserDto input)
{
var identityUser = new IdentityUser(Guid.NewGuid(), input.UserName, input.Email);
var identityResult = await userManager.CreateAsync(identityUser).ConfigureAwait(false);
if (identityResult.Succeeded)
{
//await userManager.SetPhoneNumberAsync(identityUser, input.PhoneNumber);
await CurrentUnitOfWork.SaveChangesAsync().ConfigureAwait(false);
input.Id = identityUser.Id;
return await UpdateAsync(identityUser.Id, input);
}
return null;
}
}
identityResult.Succeeded returns true
but no record in AppUsers table
Looking here https://github.com/abpframework/abp/blob/0a1568781df03fe9c86dc38cb7d47b95389cdf0b/modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUserManager.cs the CreateAsync is handled by the base class UserManager<IdentityUser>
this code is run by the MyProject.MyModule.Web.Unified project if that is relavent.
this populates the record in the AppUsers table
public override async Task<AppUserDto> CreateAsync(CreateUpdateAppUserDto input) {
var identityUser = new IdentityUser(Guid.NewGuid(), input.UserName, input.Email);
var identityResult = await userManager.CreateAsync(identityUser).ConfigureAwait(false);
if (identityResult.Succeeded) {
//await userManager.SetPhoneNumberAsync(identityUser, input.PhoneNumber);
await CurrentUnitOfWork.SaveChangesAsync().ConfigureAwait(false);
// input.Id = identityUser.Id;
// return await UpdateAsync(identityUser.Id, input);
var ret = new AppUserDto {
Id = identityUser.Id,
Name = input.Name,
Surname = input.Surname,
Gender = input.Gender,
Email = input.Email
};
return await Task.FromResult(ret);
}
return null;
}
when these are uncommented
input.Id = identityUser.Id; return await UpdateAsync(identityUser.Id, input);
this error is logged in AbpAuditLogs table
Volo.Abp.Domain.Entities.EntityNotFoundException: There is no such an entity. Entity type: MyProject.CoreModule.AppUsers.AppUser, id: b1a8f71d-8dac-40ec-ba0e-a8e9249c6263 at MyProject.CoreModule.AppUsers.AppUserAppService.CreateAsync(CreateUpdateAppUserDto input) in D:\dev\source\repos...\MyProject.CoreModule\src\MyProject.CoreModule.Application\AppUsers\AppUserAppService.cs:line 50
looks like EntityFramework tracking issue
No need to call UpdateAsync, it already saves changes when your method ends.
Also, you don't need to use ConfigureAwait in your applications. It is only needed for shared libraries.
Thanks for your response.
I eventually found this.
this is for anyone else who is searching for similar stuff.
public class AppUserAppService :
CrudAppService<AppUser, AppUserDto, Guid, PagedAndSortedResultRequestDto,
AppUserCreateDto, AppUserUpdateDto>,
IAppUserAppService
{
private readonly IdentityUserManager userManager;
public AppUserAppService(IAppUserRepository repository, IdentityUserManager userManager)
: base(repository)
{
this.userManager = userManager;
}
[Authorize(IdentityPermissions.Users.Create)]
public override async Task<AppUserDto> CreateAsync(AppUserCreateDto input) {
var user = new IdentityUser(GuidGenerator.Create(), input.UserName, input.Email);
(await userManager.CreateAsync(user).ConfigureAwait(false)).CheckErrors();
await updateUserByInput(user, input).ConfigureAwait(false);
await CurrentUnitOfWork.SaveChangesAsync().ConfigureAwait(false);
return ObjectMapper.Map<IdentityUser, AppUserDto>(user);
}
[Authorize(IdentityPermissions.Users.Update)]
public override async Task<AppUserDto> UpdateAsync(Guid id, AppUserUpdateDto input) {
...
}
}
and consume it like so
public class StaffAppService :
CrudAppService<Staff, StaffDto, Guid, PagedAndSortedResultRequestDto,
StaffCreateDto, StaffUpdateDto>,
IStaffAppService {
...
public override async Task<StaffDto> CreateAsync(StaffCreateDto input) {
var appUserAppService = serviceProvider.GetService<IAppUserAppService>();
var appUserDto = await appUserAppService.CreateAsync(input);
input.AppUserId = appUserDto.Id;
return await base.CreateAsync(input);
}
public override async Task<StaffDto> UpdateAsync(Guid id, StaffUpdateDto input) {
...
}
}