Base solution for your next web application
Open Closed

How to set up an event with a return value in the event bus? #1104


User avatar
0
colinplater created

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.


1 Answer(s)
  • User Avatar
    0
    colinplater created

    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.