Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC

Activities of "colinplater"

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.

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!

Showing 1 to 3 of 3 entries