Skip to content

Instantly share code, notes, and snippets.

@thiagoloureiro
Created March 19, 2018 21:35
Show Gist options
  • Save thiagoloureiro/09ad7ded1386b3090211a238db4de509 to your computer and use it in GitHub Desktop.
Save thiagoloureiro/09ad7ded1386b3090211a238db4de509 to your computer and use it in GitHub Desktop.
[Produces("application/json")]
[Route("api/Sample")]
public class SampleController : Controller
{
private readonly IHystrixCommand _hystrixCommand;
public SampleController(IHystrixCommandFactory hystrixCommandFactory)
{
_hystrixCommand = hystrixCommandFactory.GetHystrixCommand("GrupoTeste", "ComandoTeste");
}
[HttpGet]
public async Task<IActionResult> Get()
{
var result = await _hystrixCommand.ExecuteAsync(
async () =>
{
var rnd = new Random();
await Task.Delay(rnd.Next(500, 1000));
if (rnd.Next(4) == 0) // Quando o valor for zero falha a retorna o fallback ao invés de cair na exception e retorar um server error
{
throw new Exception("Exception, pode logar, porém cairá no fallback e o processo continuará normalmente...");
}
return "Resultado Válido";
},
() => Task.FromResult("Resultado Fallback"),
new CancellationTokenSource());
return Ok($"Hello! Resultado: {result}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment