Polly resilience example in ASP.NET Core Web API
You can add resilience to your ASP.NET Core application using Polly to retry failed downstream API calls.
Learn more about adding resilience using Polly, or watch the video.
Download the code example
You'll need to fill out the code example form. We will send you an email where you can download the code example.
Software
This is the software that will need to be installed onto your machine.
Visual Studio 2026. Version 18.0.0 or above. It will work with the free community version.
.NET 10 SDK. Version 10.0.0 or above.
The application
Open up RoundTheCode.PollyExample.slnx in Visual Studio. There is an Api and DownstreamApi project. Both of these need to be running for this to work.
Right-click on
Solution 'RoundTheCode.PollyExample'in Solution Explorer.Select Configure Startup Projects...
Select Multiple startup projects:
Select the Action for
ApiandDownstreamApito Start
Run the application. The Swagger documentation for Api will load at https://localhost:7275/swagger. DownstreamApi should also be running on https://localhost:7276, but it won't load the Swagger documentation.
Here is what you can test in Api:
/api/resilience/retry/constant- Will make three attempts toDownstreamApiwith a 2-second delay between each one. The last one will return a success status code./api/resilience/retry/exponential- Will make four attempts toDownstreamApi. Each retry attempt will be multiplied by 2 seconds. The last one will return a success status code./api/resilience/retry/linear- Will make four attempts toDownstreamApi. Each retry attempt will be add an additional 2 seconds. The last one will return a success status code./api/resilience/retry/jitter- Will make four attempts toDownstreamApi. Each retry attempt will add a random delay. The last one will return a success status code./api/resilience/retry/circuit-breaker- Will make five attempts toDownstreamApi. On the fifth attempt, an internal server error is returned stating that the circuit breaker is in action. For the next 20 seconds, any calls to this endpoint will throw this exception. After 20 seconds, it should return a success status code./api/resilience/retry/timeout- Will make an attempt toDownstreamApi. The request will timeout after 5 seconds and will return an internal server error stating that there was no response within the timeout interval.
Download this code example
Enter your email and we'll send you a download link.
Related pages
Learn the best practices on how to use HttpClient correctly and avoid socket exceptions by using HttpClientFactory in an ASP.NET Core Web API.
Download this code example
Enter your email and we'll send you a download link.