Skip to content

VisualBean/ResilientCommand

Repository files navigation

.Net NuGet version

ResilientCommand

A Resiliency library for wrapping dependency calls. Heavily inspired by Hystrix

The idea is simple; protect against up/down- stream problems, by encapsulating calls to these dependencies in reliability patterns. This project seeks to do just that.

There is a Getting-Started Guide here.
Please check the wiki for more information or the examples some usage examples.

Getting started

Step 1:

Download the NuGet package here

Install-Package ResilientCommand

Step 2:

Either add your commands as singletons/scoped yourself or use the helper

services.AddResilientCommands(typeof(Startup));

The above will look through the assembly for all Resilient Commands.

Step 3:

Create a command for your specific scenario (in this particular example we are using the HttpClientFactory dependency injection

class GetUsersCommand : ResilientCommand<IEnumerable<User>>
{
    private readonly HttpClient client;

    public GetUsersCommand(HttpClient client)
    {
        this.client = client;
    }
    protected override async Task<IEnumerable<User>> RunAsync(CancellationToken cancellationToken)
    {
        var response = await client.GetAsync("api/users");
        response.EnsureSuccessStatusCode();

        var content = await response.Content.ReadAsStringAsync();
        return JsonConvert.DeserializeObject<IEnumerable<User>>(content);
    }
}

Step 4:

Inject and use your command.

public class MyService()
{
    private UsersCommand usersCommand;

    public MyService(UsersCommand usersCommand)
    {
        this.usersCommand = usersCommand;
    }

    public async Task<IEnumerable<Users>>GetUsers(CancellationToken cancellationToken)
    {
        return await this.usersCommand.ExecuteAsync(cancellationToken);
    }
}