ASP.NET Core Interview Question #231
How would you safely call an unreliable external API from ASP.NET Core?
Senior ASP.NET Core Real-World Scenarios Senior Advanced
Quick Interview Answer
Use IHttpClientFactory, set an appropriate timeout, propagate cancellation, and classify failures. Apply retries only to transient failures and only when retrying the operation is safe. Backoff and jitter help avoid synchronized retry storms.
Detailed Explanation
Use IHttpClientFactory, set an appropriate timeout, propagate cancellation, and classify failures. Apply retries only to transient failures and only when retrying the operation is safe. Backoff and jitter help avoid synchronized retry storms.
Use circuit breaking or similar resilience mechanisms when repeated downstream failures would otherwise consume application resources. Validate responses, log useful correlation information, and avoid exposing downstream secrets or raw internal errors to callers.
For non-idempotent downstream operations, retries require special care because the first attempt may have succeeded even if the response was lost.
Code Example
builder.Services.AddHttpClient<PaymentClient>(client =>
{
client.BaseAddress = new Uri("https://payments.example/");
client.Timeout = TimeSpan.FromSeconds(10);
});
// Add an appropriate resilience strategy for the operation.