What is the current guidance for async/await APIs to replace .NET events?

Consider the case of a library intended for use with other projects, which could be console applications, other class libraries, or applications with a UI. This library allows the user to send commands to and receive responses from a remote end point, but can also receive event messages from the remote end point. It is a valid use case that the user may send a command in response to a remote event. The transport mechanism uses async/await methods for transferring data to and from the remote end (e.g., using a WebSocket). In a “classic” .NET application, I might design an API like the following:

private Command currentCommand;

public event EventHandler<EventReceivedEventArgs>? EventReceived;

public CommandResponse ExecuteCommand(CommandParameters parameters)
{
    currentCommand = transport.SendData(parameters.Serialize());
    return currentCommand.WaitForCommandResponse();
}

protected void OnEventReceived(EventData eventData)
{
    if (this.EventReceived is not null)
    {
        this.EventReceived(this, new EventReceivedEventArgs(eventData));
    }
}

private void DataReceiver()
{
    // Example only. In a real app, there would be some terminating
    // condition here.
    while (true)
    {
        // For discussion purposes, assume this an API that returns data
        // from the remote end. How it works and how it is parsed can be
        // considered an implementation detail.
        byte[] receivedData = transport.ReceiveData();
        ParsedData parsed = Parse(receivedData);
        if (parsed is CommandResponse)
        {
            this.currentCommand.SetResponse(parsed);
        }

        if (parsed is EventData)
        {
            this.OnEventReceived(parsed);
        }
    }
}

However, given that I’ve heard repeatedly (and today, somewhat pointedly), “.NET events don’t work with async/await,” “Don’t use async void, ever,” and, “Don’t mix synchronous and async code.” So given the async/await form of the API looks like:

private Command currentCommand;


// This, right here, I'm told, is bad, bad, bad. Totally suspect.
// Poor API design choice. Don't do it.
public event EventHandler<EventReceivedEventArgs>? EventReceived;

public async Task<CommandResponse> ExecuteCommandAsync(CommandParameters parameters)
{
    await transport.SendCommandAsync(parameters.Serialize());
    return await transport.WaitForCommandResponse();
}

protected void OnEventReceived(EventData eventData)
{
    if (this.EventReceived is not null)
    {
        this.EventReceived(this, new EventReceivedEventArgs(eventData));
    }
}

private async Task DataReceiver()
{
    while (true)
    {
        await byte[] receivedData = transport.ReceiveData();
        ParsedData parsed = Parse(receivedData);
        if (parsed is CommandResponse)
        {
            this.currentCommand.SetResponse(parsed);
        }

        if (parsed is EventData)
        {
            this.OnEventReceived(parsed);
        }
    }

    return Task.CompletedTask;
}

I totally understand why this is suboptimal. The event handler of the consumer will block the producing method. It might hang or be very long-running. It might throw. There are any of a myriad of things that might be problematic with this style of API.

But here’s the operative question: What’s the alternative? What does the .NET developer community recommend as a way to surface this type of capability to the user instead of using .NET events?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật