Created
March 19, 2018 21:35
-
-
Save thiagoloureiro/09ad7ded1386b3090211a238db4de509 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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