Skip to content

STREAMS - Refresh Timer

There are scenarios where you want to have data requests be called every so often. The RefreshTimer middleware is designed to help you with this.

Attribute Setup

public class MyRequest : IStreamRequest<string>;
[RefreshTimer(3000)] // every 3 seconds
public class MyStreamRequestHandler : IStreamRequestHandler<MyRequest, string>
{
public async IAsyncEnumerable<string> Handle(MyRequest request, [EnumeratorCancellation] CancellationToken cancellationToken)
{
yield return DateTimeOffset.Now.ToString("h:mm:ss tt");
}
}

and now in your code, you can simply call the mediator to get the stream and await foreach

```csharp
var stream = mediator.Request(new MyRequest());
await foreach (var item in stream)
{
// this code will be called every 3 seconds
}

Configuration

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

{
"Mediator": {
"TimerRefresh": {
"MyNamespace.MyStreamHandler": {
"IntervalSeconds": 20
}
}
}
}