Table of Contents

Class MessageAsyncEnumerable<T>

Namespace
Savvyio.Messaging
Assembly
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

T

The 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

MessageAsyncEnumerable(IAsyncEnumerable<IMessage<T>>, Action<MessageAsyncEnumerableOptions<T>>)

Initializes a new instance of the MessageAsyncEnumerable<T> class.

public MessageAsyncEnumerable(IAsyncEnumerable<IMessage<T>> source, Action<MessageAsyncEnumerableOptions<T>> setup = null)

Parameters

source IAsyncEnumerable<IMessage<T>>

The sequence to iterate.

setup Action<MessageAsyncEnumerableOptions<T>>

The MessageAsyncEnumerableOptions<T> which may be configured.

Exceptions

ArgumentNullException

source cannot be null.

ArgumentException

setup failed to configure an instance of MessageAsyncEnumerableOptions<T> in a valid state.

MessageAsyncEnumerable(IEnumerable<IMessage<T>>, Action<MessageAsyncEnumerableOptions<T>>)

Initializes a new instance of the MessageAsyncEnumerable<T> class.

public MessageAsyncEnumerable(IEnumerable<IMessage<T>> source, Action<MessageAsyncEnumerableOptions<T>> setup = null)

Parameters

source IEnumerable<IMessage<T>>

The sequence to iterate.

setup Action<MessageAsyncEnumerableOptions<T>>

The MessageAsyncEnumerableOptions<T> which may be configured.

Methods

GetAsyncEnumerator(CancellationToken)

Returns an enumerator that iterates asynchronously through the collection.

public virtual IAsyncEnumerator<IMessage<T>> GetAsyncEnumerator(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A CancellationToken that may be used to cancel the asynchronous iteration.

Returns

IAsyncEnumerator<IMessage<T>>

An enumerator that can be used to iterate asynchronously through the collection.

See Also