Skip to content

Should Events by async, and should I await the invocation? #8

@ppittle

Description

@ppittle

Curious best practice or pitfalls around wanting to have my event handlers be async:

ICustomerManagementService service;
service.CustomerRegistered += async (info) => await ...

This will require the event definition to support.

Modifying the example on the readme.md:

public class CustomerManagementService : ICustomerManagementService
{
  // now it's an async event:
  public virtual event Func<CustomerInfo, Task> CustomerRegistered;

  public virtual async Task RegisterCustomer(CustomerInfo customerInfo)
  {
    // ... (register a new customer in the system here)

    // Invoke event.  But should I await or not?
    
     CustomerRegistered?.Invoke(this, customerInfo);
     // OR
     await CustomerRegistered?.Invoke(this, customerInfo);
  }
}

Does it matter either way?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions