Skip to content

Instantly share code, notes, and snippets.

@maiconcp
Last active May 1, 2019 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maiconcp/d1b1c5355bfc41ac3a108adac7b4d14b to your computer and use it in GitHub Desktop.
Save maiconcp/d1b1c5355bfc41ac3a108adac7b4d14b to your computer and use it in GitHub Desktop.
public class ProductService
{
private readonly HttpClient _httpClient;
private readonly AsyncCircuitBreakerPolicy _circuitBreakerPolicy;
public ProductService()
{
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri("http://localhost:5000/");
_circuitBreakerPolicy = Policy.Handle<HttpRequestException>()
.CircuitBreakerAsync(exceptionsAllowedBeforeBreaking: 2,
durationOfBreak: TimeSpan.FromMinutes(1));
}
public async Task<string> GetSample()
{
return await _circuitBreakerPolicy.ExecuteAsync<string>(async () =>
{
return await _httpClient.GetStringAsync("api/business");
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment