Last active
June 8, 2018 07:43
-
-
Save christiannagel/2eae008ea7387a1dec991b8400bc490f to your computer and use it in GitHub Desktop.
Using Polly with HttpClient
This file contains 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
public PolicySample() => AppServices = ConfigureServices(); | |
private ServiceProvider ConfigureServices() | |
{ | |
var services = new ServiceCollection(); | |
services.AddLogging(builder => | |
{ | |
builder.AddFilter((category, level) => true); | |
builder.AddConsole(options => options.IncludeScopes = true); | |
}); | |
services.AddHttpClient("cni", client => | |
{ | |
client.BaseAddress = new Uri("https://www.cninnovation1.com"); // not found server | |
}) | |
//.AddPolicyHandler(Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(10))) | |
.AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(new [] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(10) })) // network failures, HTTP 5xx, HTTP 408 | |
.AddTypedClient<PolicyClient>(); | |
return services.BuildServiceProvider(); | |
} | |
public IServiceProvider AppServices { get; private set; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment