Skip to content

REQUESTS - Resiliency

Resiliency is a necessary thing to do discuss when it comes to mobile & network calls. Cell signals can drop, wifi can be spotty, and servers can be down.
The Resilience middleware is designed to help you handle these situations. Shiny Mediator provides easy middleware to the actual workhorse in this equation - Microsoft.Extension.Resilience. This library provides a number of resiliency patterns such as Circuit Breakers, Retry Policies, and more.

For more documentation on the Microsoft.Extension.Resilience library, please see the official documentation

Setup

  1. Install the Shiny.Mediator.Resilience package from NuGet

  2. Add the resilience middleware to your mediator setup

    services.AddShinyMediator(cfg =>
    {
    cfg.AddResiliencyMiddleware(
    ("test", builder =>
    {
    builder.AddRetry(new RetryStrategyOptions());
    builder.AddTimeout(TimeSpan.FromSeconds(2.0));
    })
    );
    });
  3. With any request handler that provides a result, you can now add the following attribute

    public class MyRequestHandler : IRequestHandler<MyResponse, MyResponse>
    {
    [Resilient("test")]
    public async Task<MyResponse> Handle(MyResponse request, CancellationToken cancelToken)
    {
    // do your work here
    var result = await SomeNetworkCallThatTakesForever(cancelToken);
    return result;
    }
    }

That’s it! Your request handler is now resilient!