Learn Minimal APIs from start to finish. Perfect for beginners.

Polly resilience example in ASP.NET Core Web API

David Grace David Grace

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.

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 Api and DownstreamApi to 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 to DownstreamApi with a 2-second delay between each one. The last one will return a success status code.

  • /api/resilience/retry/exponential - Will make four attempts to DownstreamApi. 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 to DownstreamApi. 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 to DownstreamApi. 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 to DownstreamApi. 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 to DownstreamApi. 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.

Download this code example

Enter your email and we'll send you a download link.