---
title: "How to integrate Polly resilience policies with HttpClientFactory in ASP.NET Core?"  
description: "How to integrate Polly resilience policies with HttpClientFactory in ASP.NET Core?"  
author: "Ravi Vishwakarma"  
published: 2026-08-17  
updated: 2026-08-17  
canonical: https://answers.mindstick.com/qa/117066/how-to-integrate-polly-resilience-policies-with-httpclientfactory-in-asp-net-core  
category: "Polly"  
tags: ["aspnet-core", "dotnet", "polly", "httpclient", "web-api"]  
reading_time: 2 minutes  

---

# How to integrate Polly resilience policies with HttpClientFactory in ASP.NET Core?

ASP.NET Core integrates natively with Polly via the `Microsoft.Extensions.Http.Resilience` package, simplifying HTTP resilience configuration.

## Standard Resilience Handler Integration

You can apply standard resilience pipelines directly to registered HTTP clients in `Program.cs`.

### Code Example

```cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddHttpClient("PaymentService", client =>
{
    client.BaseAddress = new Uri("https://api.paymentservice.com/");
})
.AddStandardResilienceHandler(options =>
{
    options.Retry.MaxRetryAttempts = 3;
    options.Retry.BackoffType = DelayBackoffType.Exponential;
    options.AttemptTimeout.Timeout = TimeSpan.FromSeconds(5);
    options.CircuitBreaker.SamplingDuration = TimeSpan.FromSeconds(30);
});
```

### Benefits

- Automatic configuration of retries, circuit breakers, timeouts, and hedging.
- Native integration with `IHttpClientFactory` lifecycle.

## Answers

### Answer by Ravi Vishwakarma

Integrating **Polly** resilience policies with **HttpClientFactory** in ASP.NET Core allows you to build fault-tolerant HTTP clients capable of handling transient network failures, service outages, and slow responses seamlessly.

## Prerequisites

To use Polly with `HttpClientFactory`, install the official Microsoft integration package via NuGet:

```plaintext
dotnet add package Microsoft.Extensions.Http.Polly
```

## 1. Defining Resilience Policies

Polly provides various policy types such as **Retry**, **Circuit Breaker**, and 00**Timeout**. Using the `HttpPolicyExtensions` helper class, you can easily target transient HTTP errors (5xx status codes and 408 Request Timeout, or `HttpRequestException`).

### Defining a Retry Policy with Exponential Backoff

```cs
using Polly;
using Polly.Extensions.Http;
using System;
using System.Net.Http;

public static class PollyPolicies
{
    public static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
    {
        return HttpPolicyExtensions
            .HandleTransientHttpError()
            .WaitAndRetryAsync(3, retryAttempt =>
                TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
    }
}
```

### Defining a Circuit Breaker Policy

```cs
public static IAsyncPolicy<HttpResponseMessage> GetCircuitBreakerPolicy()
{
    return HttpPolicyExtensions
        .HandleTransientHttpError()
        .CircuitBreakerAsync(5, TimeSpan.FromSeconds(30));
}
```

## 2. Registering Policies with HttpClientFactory

In your `Program.cs` file, register your named or typed HTTP client using `AddHttpClient` and attach the resilience policies using `AddPolicyHandler`.

```cs
var builder = WebApplication.CreateBuilder(args);

// Register HttpClient with Polly Retry Policy
builder.Services.AddHttpClient("ExternalApiClient", client =>
{
    client.BaseAddress = new Uri("https://api.example.com/");
    client.DefaultRequestHeaders.Add("Accept", "application/json");
})
.AddPolicyHandler(PollyPolicies.GetRetryPolicy())
.AddPolicyHandler(PollyPolicies.GetCircuitBreakerPolicy());

var app = builder.Build();
```

## 3. Consuming the HttpClient

Inject `IHttpClientFactory` into your services or controllers and create client instances normally. The Polly policies will execute automatically around each request execution.

```cs
public class WeatherService
{
    private readonly IHttpClientFactory _httpClientFactory;

    public WeatherService(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }

    public async Task<string> GetWeatherDataAsync()
    {
        var client = _httpClientFactory.CreateClient("ExternalApiClient");
        var response = await client.GetAsync("weather");
        response.EnsureSuccessStatusCode();

        return await response.Content.ReadAsStringAsync();
    }
}
```

## Conclusion

By coupling `IHttpClientFactory` with Polly policies, you centralize transient error handling, enhance system dependability, and keep HTTP service interaction logic clean and maintainable.


---

Original Source: https://answers.mindstick.com/qa/117066/how-to-integrate-polly-resilience-policies-with-httpclientfactory-in-asp-net-core

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
