MessageAsyncEnumerable<T> Class
Definition
- Assemblies
- Savvyio.Messaging.dll
Exposes an enumerator that provides asynchronous iteration over values of a specified type.
public class MessageAsyncEnumerable<T> : IAsyncEnumerable<IMessage<T>> where T : IRequest
Type Parameters
TThe type of the elements in the collection.
- Inheritance
-
MessageAsyncEnumerable<T>
- Implements
Examples
MessageAsyncEnumerable<T> enables async enumeration over a stream of IMessage<T> envelopes from a queue or bus. To use it, pass an async callback and options to its constructor; the callback is invoked for each page of messages. The example creates an enumerator over a small in-memory sequence to show the enumeration contract.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Savvyio;
using Savvyio.Messaging;
namespace ExampleApp;
public sealed class MessageAsyncEnumerableExample
{
public async Task<int> ProcessAsync()
{
var messages = new[]
{
new Message<CreateOrderCommand>("msg-42", new Uri("urn:orders"), "orders.created", new CreateOrderCommand("ORD-42"))
};
var stream = new MessageAsyncEnumerable<CreateOrderCommand>(messages, options =>
{
options.MessageCallback = async message => await message.AcknowledgeAsync().ConfigureAwait(false);
options.AcknowledgedPropertiesCallback = async acknowledged => await Task.CompletedTask.ConfigureAwait(false);
});
var count = 0;
await foreach (var message in stream.ConfigureAwait(false))
{
count++;
}
return count;
}
}
public sealed record CreateOrderCommand(string OrderId) : Request;
Constructors
| Name | Description |
|---|---|
| MessageAsyncEnumerable(IAsyncEnumerable<IMessage<T>>, Action<MessageAsyncEnumerableOptions<T>>) | Initializes a new instance of the MessageAsyncEnumerable<T> class. |
| MessageAsyncEnumerable(IEnumerable<IMessage<T>>, Action<MessageAsyncEnumerableOptions<T>>) | Initializes a new instance of the MessageAsyncEnumerable<T> class. |
Methods
| Name | Description |
|---|---|
| GetAsyncEnumerator(CancellationToken) | Returns an enumerator that iterates asynchronously through the collection. |