Table of Contents

Class AsyncCollectionRequestMessage<T>

Namespace
CommunityToolkit.Mvvm.Messaging.Messages
Assembly
CommunityToolkit.Mvvm.dll

A class for request messages that can receive multiple replies, which can either be used directly or through derived classes.

public class AsyncCollectionRequestMessage<T> : IAsyncEnumerable<T>

Type Parameters

T

The type of request to make.

Inheritance
AsyncCollectionRequestMessage<T>
Implements
Inherited Members

Constructors

AsyncCollectionRequestMessage()

public AsyncCollectionRequestMessage()

Properties

CancellationToken

Gets the CancellationToken instance that will be linked to the one used to asynchronously enumerate the received responses. This can be used to cancel asynchronous replies that are still being processed, if no new items are needed from this request message. Consider the following example, where we define a message to retrieve the currently opened documents:

public class OpenDocumentsRequestMessage : AsyncCollectionRequestMessage<XmlDocument> { }

We can then request and enumerate the results like so:

await foreach (var document in Messenger.Default.Send<OpenDocumentsRequestMessage>())
{
    // Process each document here...
}

If we also want to control the cancellation of the token passed to each subscriber to the message, we can do so by passing a token we control to the returned message before starting the enumeration (WithCancellation<T>(IAsyncEnumerable<T>, CancellationToken)). The previous snippet with this additional change looks as follows:

await foreach (var document in Messenger.Default.Send<OpenDocumentsRequestMessage>().WithCancellation(cts.Token))
{
    // Process each document here...
}

When no more new items are needed (or for any other reason depending on the situation), the token passed to the enumerator can be canceled (by calling Cancel()), and that will also notify the remaining tasks in the request message. The token exposed by the message itself will automatically be linked and canceled with the one passed to the enumerator.

public CancellationToken CancellationToken { get; }

Property Value

CancellationToken

Methods

GetResponsesAsync(CancellationToken)

Gets the collection of received response items.

public Task<IReadOnlyCollection<T>> GetResponsesAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A CancellationToken value to stop the operation.

Returns

Task<IReadOnlyCollection<T>>

The collection of received response items.

Reply(Func<CancellationToken, Task<T>>)

Replies to the current request message.

public void Reply(Func<CancellationToken, Task<T>> response)

Parameters

response Func<CancellationToken, Task<T>>

The response to use to reply to the request message.

Exceptions

ArgumentNullException

Thrown if response is null.

Reply(Task<T>)

Replies to the current request message.

public void Reply(Task<T> response)

Parameters

response Task<T>

The response to use to reply to the request message.

Exceptions

ArgumentNullException

Thrown if response is null.

Reply(T)

Replies to the current request message.

public void Reply(T response)

Parameters

response T

The response to use to reply to the request message.