Another problem: After creating the tenant (TenantAppService.CreateAsync
), I would like to change something about the user. But it seems that the user is not yet in the database. It is not (yet) found...
Is the transaction only completed after all EventHandlers have been handled? Or is this a timing problem?
public class TenantEventHandler : IDistributedEventHandler<TenantCreatedEto>, ITransientDependency
{
private readonly IdentityUserManager _userManager;
private readonly ICurrentTenant _currentTenant;
public TenantEventHandler(IdentityUserManager userManager, ICurrentTenant currentTenant)
{
_userManager = userManager;
_currentTenant = currentTenant;
}
public async Task HandleEventAsync(TenantCreatedEto eventData)
{
using (_currentTenant.Change(eventData.Id))
{
// Set admin user as confirmed
var adminUser = await _userManager.FindByEmailAsync(PalmaConsts.AdminEmailDefaultValue);
if (adminUser != null)
{
adminUser.SetEmailConfirmed(true);
await _userManager.UpdateAsync(adminUser);
}
}
}
Ok, I've seen the problem. It's because my TenantEventHandler
was called first and then PalmaTenantDatabaseMigrationHandler
, which creates the admin user.
Probably you can't specify a order?
I would simply prefer to have my logic in a separate EventHandler, as there are also further steps (sending mail etc.) which have nothing to do with database migration...
public async Task HandleEventAsync(TenantCreatedEto eventData)
{
await MigrateAndSeedForTenantAsync(
eventData.Id,
eventData.Properties.GetOrDefault("AdminEmail") ?? PalmaConsts.AdminEmailDefaultValue,
eventData.Properties.GetOrDefault("AdminPassword") ?? PalmaConsts.AdminPasswordDefaultValue
);
using (_currentTenant.Change(eventData.Id))
{
// Set admin user as confirmed
var adminUser = await _userManager.FindByEmailAsync(PalmaConsts.AdminEmailDefaultValue);
if (adminUser != null)
{
adminUser.SetEmailConfirmed(true);
await _userManager.UpdateAsync(adminUser);
}
}
}
You can publish an event in
PalmaTenantDatabaseMigrationHandler
Ah yes, that's it. Thank you!