AsyncActionEventHandler.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using System.Threading.Tasks;
  3. using Abp.Dependency;
  4. namespace Abp.Events.Bus.Handlers.Internals
  5. {
  6. /// <summary>
  7. /// This event handler is an adapter to be able to use an action as <see cref="IAsyncEventHandler{TEventData}"/> implementation.
  8. /// </summary>
  9. /// <typeparam name="TEventData">Event type</typeparam>
  10. internal class AsyncActionEventHandler<TEventData> :
  11. IAsyncEventHandler<TEventData>,
  12. ITransientDependency
  13. {
  14. /// <summary>
  15. /// Function to handle the event.
  16. /// </summary>
  17. public Func<TEventData, Task> Action { get; private set; }
  18. /// <summary>
  19. /// Creates a new instance of <see cref="AsyncActionEventHandler{TEventData}"/>.
  20. /// </summary>
  21. /// <param name="handler">Action to handle the event</param>
  22. public AsyncActionEventHandler(Func<TEventData, Task> handler)
  23. {
  24. Action = handler;
  25. }
  26. /// <summary>
  27. /// Handles the event.
  28. /// </summary>
  29. /// <param name="eventData"></param>
  30. public async Task HandleEventAsync(TEventData eventData)
  31. {
  32. await Action(eventData);
  33. }
  34. }
  35. }