Managed to figure it out myself. I just needed to change the calling code to this:
UpdateStatus status = UpdateStatus.Ready;
CandidateExportUpdateEventData eventData = new CandidateExportUpdateEventData { candidate = (ProfileCandidate)objectValue, status = status };
await EventBus.TriggerAsync(eventData);
return eventData.status;
Now I can retrieve the status returned from the eventData.
I am using an eventbus to handle various events in my code. Some of those events then perform actions, and need to return a value indicating whether or not the actions were successful. Here is example of one of my events:
public async void HandleEvent(ProfileService.CandidateExportUpdateEventData eventData)
{
try
{
await _candidateService.CreateOrUpdateCandidate(eventData.candidate.ToCandidate(_candidateRepository), true);
eventData.status = ProfileService.UpdateStatus.Success;
}
catch
{
eventData.status = ProfileService.UpdateStatus.FailedError;
}
}
The code which calls this looks like this:
UpdateStatus status = UpdateStatus.Ready;
await EventBus.TriggerAsync(new CandidateExportUpdateEventData { candidate = (ProfileCandidate)objectValue, status = status });
return status;
Unfortunately this always returns UpdateStatus.Ready. I am hoping someone can tell me what I need to do to get the correct status back from the event.
Hi Hikalkan. It looks like the InsertAsync is locking the database table until CreateCandidateAsync has fully completed, which means when the event tries to update the candidate it can't find it. A colleague suggested changing
_unitOfWorkManager.Current.Completed
to
_unitOfWorkManager.Current.Disposed
This ensures the event doesn't fire until after the candidate has been added to the database, but also means there is an error when attempting to update the candidate because the UserManagerProxy has been disposed of.
Is there a way I can recreate the UserManagerProxy, or stop it from being disposed of in the first place?
Hi Hikalkan. It looks like the InsertAsync is locking the database table until CreateCandidateAsync has fully completed, which means when the event tries to update the candidate it can't find it. A colleague suggested changing
_unitOfWorkManager.Current.Completed
to
_unitOfWorkManager.Current.Disposed
This ensures the event doesn't fire until after the candidate has been added to the database, but also means there is an error when attempting to update the candidate because the UserManagerProxy has been disposed of.
Is there a way I can recreate the UserManagerProxy, or stop it from being disposed of in the first place?
I am working on a website built using ASP.Net Boilerplate. I have a service containing a method, which adds a candidate to the database and then triggers an event. During the event I need to update the candidate, but I am finding that when that code runs it hasn't yet been added to the database. Here is my code:
[AbpAuthorize(RecruitmentPermissions.Recruitment_Candidates_Create)]
protected virtual async Task CreateCandidateAsync(CandidateEditDto input, bool suppressEvents = false)
{
var candidate = input.MapTo<Candidate>();
candidate.Id = Guid.NewGuid();
if (candidate.User.TenantId == null)
{
Abp.Authorization.Users.UserRole userRole = new Abp.Authorization.Users.UserRole()
{
RoleId = 3
};
candidate.User.TenantId = AbpSession.TenantId;
candidate.User.Name = input.FirstName;
candidate.User.Surname = input.Surname;
candidate.User.UserName = input.Username ?? input.EmailAddress;
candidate.User.Password = new PasswordHasher().HashPassword(input.Password);
candidate.User.IsActive = true;
candidate.User.EmailAddress = input.EmailAddress;
candidate.User.Roles = new List<Abp.Authorization.Users.UserRole>()
{
userRole
};
candidate.User.CreatorUserId = AbpSession.UserId;
}
// wait for insertion before firing event
_unitOfWorkManager.Current.Completed += (sender, args) =>
{
if (!suppressEvents)
EventBus.Trigger(new InsertCandidateEventData { candidate = candidate });
};
await _candidateRepository.InsertAsync(candidate);
}
I have tried using UnitOfWorkManager.Current.Completed, as the documentation suggests that any code inside the { } will be fired after insert has completed - but this is not the case. I have also tried using UnitOfWorkManager.Current.SaveChanges, to force the candidate to be added to the database before firing the event, but again that makes no difference.
I am quite new to the asp.net boilerplate, so any suggestions anyone might have would be greatly appreciated. Thanks
I have some code which inserts a candidate and then triggers an event to perform some other actions. Within the event there is code which should update the candidate, but it always fails because the candidate isn't being saved to the database until after the event. Here is the code:
[AbpAuthorize(RecruitmentPermissions.Recruitment_Candidates_Create)]
protected virtual async Task CreateCandidateAsync(CandidateEditDto input, bool suppressEvents = false)
{
var candidate = input.MapTo<Candidate>();
candidate.Id = Guid.NewGuid();
if (candidate.User.TenantId == null)
{
Abp.Authorization.Users.UserRole userRole = new Abp.Authorization.Users.UserRole()
{
RoleId = 3
};
candidate.User.TenantId = AbpSession.TenantId;
candidate.User.Name = input.FirstName;
candidate.User.Surname = input.Surname;
candidate.User.UserName = input.Username ?? input.EmailAddress;
candidate.User.Password = new PasswordHasher().HashPassword(input.Password);
candidate.User.IsActive = true;
candidate.User.EmailAddress = input.EmailAddress;
candidate.User.Roles = new List<Abp.Authorization.Users.UserRole>()
{
userRole
};
candidate.User.CreatorUserId = AbpSession.UserId;
}
// wait for insertion before firing event
_unitOfWorkManager.Current.Completed += (sender, args) =>
{
if (!suppressEvents)
EventBus.Trigger(new InsertCandidateEventData { candidate = candidate });
};
await _candidateRepository.InsertAsync(candidate);
}
According to the documentation, after InsertAsync has been run the unitOfWorkManager should then trigger my event - but it doesn't. I've tried saving manually (_unitOfWorkManager.Current.SaveChanges), and also putting the insert into it's own unit of work, but the database just isn't updated until CreateCandidateAsync has completed.
I'm quite new to ASP.Net Boilerplate, so any help anyone can give would be greatly appreciated. Thanks!