Skip to content

STREAMS - Replay

There are occasions where you need to make a large or long duration network call, what if you could return the previous results while the current network call is in progress? What if you don’t have a network connection and the method should just “wait” until it does? The Replay middleware is designed to help you with this.

Setup

So here we have a standard HTTP call on our streaming async enumerable protocol. Nothing special here, but once we turn on the Replay middleware, we can return the previous results while the current network call is in progress.

public class MyRequest : IStreamRequest<string>;
public class MyStreamRequestHandler : IStreamRequestHandler<MyRequest, SomeResponse>
{
public async IAsyncEnumerable<string> Handle(MyRequest request, [EnumeratorCancellation] CancellationToken cancellationToken)
{
... some long http call
}
}

In Your Code

At first, you’ll see there is nothing special, but the cool part here is that the async enumerable will “pump” twice. The first time for the previous results (if the call has previous been made) and the second time with the new response from your long call.

var stream = mediator.Request(new MyRequest());
await foreach (var item in stream)
{
MyBinding = item; // will passthrough here twice
}

Configuration

Much like other middleware, you can use the Microsoft.Extensions.Configuration.

{
"Mediator": {
"ReplayStream": {
"MyNamespace.MyStreamHandler": true
}
}
}