Skip to content

Use AsyncEventHandler<MessagingEvent> for consumer message handling - #45

Merged
bebelz merged 1 commit into
masterfrom
consumer-async-handler
Jun 9, 2026
Merged

bebelz merged 1 commit into
masterfrom
consumer-async-handler

Conversation

@bebelz

@bebelz bebelz commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Converts the consumer's ConsumeAsync callback from the synchronous EventHandler<IMessagingEvent> to RabbitMQ's AsyncEventHandler<MessagingEvent>, awaited inside ReceivedAsync — so message handlers can do real async work instead of being run synchronously.

Changes

  • MerQure.RbMQ/Clients/Consumer.csConsumeAsync takes AsyncEventHandler<MessagingEvent> and awaits it inside the now-async ReceivedAsync handler. Serialization switches from lock to SemaphoreSlim(1,1) (also in StopConsuming) since you can't await inside a lock.
  • MerQure/Events/MessagingEvent.cs (new, public) — moved out of MerQure.RbMQ and now derives from AsyncEventArgs so it satisfies the where TEvent : AsyncEventArgs constraint on RabbitMQ's delegate.
  • MerQure.RbMQ/Events/MessagingEvent.cs — deleted (the internal version).
  • MerQure/Clients/IConsumer.cs — interface signature updated.
  • MerQure.Tools/Buses/Consumer.cs — caller lambda returns Task.CompletedTask.
  • tests/MerQure.Tools.Tests/Buses/ConsumerTests.cs — stub updated to the new delegate type.
  • samples/ — example handlers return the ack/nack Task to match the async signature.

Note

MessagingEvent now leaks a RabbitMQ.Client base type (AsyncEventArgs) into the MerQure abstraction layer. This was a deliberate choice to use RabbitMQ's AsyncEventHandler<T> directly rather than a Func<object, IMessagingEvent, Task>.

Testing

  • All projects, samples, and the test suite build cleanly.
  • dotnet test — 29 passed, 0 failed.

Convert the consumer's ConsumeAsync callback from the synchronous
EventHandler<IMessagingEvent> to RabbitMQ's AsyncEventHandler<MessagingEvent>,
awaited inside ReceivedAsync. MessagingEvent moves into the MerQure project
and derives from AsyncEventArgs so it satisfies the delegate constraint.
Serialization switches from a lock to a SemaphoreSlim to allow awaiting.
@bebelz
bebelz requested review from Poltuu, blemasle and rducom as code owners June 9, 2026 13:17

@Poltuu Poltuu left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

j'avoue je maitrise pas les tenants et aboutissants

/// </summary>
/// <param name="onMessageReceived">Handler called each time a message arrives for this consumer.</param>
Task ConsumeAsync(EventHandler<IMessagingEvent> onMessageReceived);
Task ConsumeAsync(AsyncEventHandler<MessagingEvent> onMessageReceived);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

breaking mais j'imagine que c'est pas grave ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est justement le but même. J'avais oublié cet usage lors de la release précédente donc on se retrouve avec du sync over async en Consumer

@bebelz
bebelz merged commit d19bea3 into master Jun 9, 2026
4 checks passed
@bebelz
bebelz deleted the consumer-async-handler branch June 9, 2026 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants