-1

We are developing Azure function in .Net6 which is interacting with multiple 3rd party application over HTTP protocol.

We have implemented Polly to handle transient errors.

The only issue with Polly is that you have to wrap the code with the policy to make it resilient.

var policy = Policy
          .Handle<SomeExceptionType>()
          .Retry();
policy.Execute(() => DoSomething());

That's why we are also exploring other options e.g implementing resiliency at protocol level like "HTTP".

So the idea is, instead of wrapping policy around code if somehow we can implement resiliency at protocol level so that all request over HTTP protocol must use the same resiliency policy.

This way we don't have to wrap policy around code every time to make it resilient. Any help would be highly appreciated.

Not sure if interceptor would help here!!!

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Sunil
  • 17
  • 3

1 Answers1

0

If you are using HttpClient for your Http communication then you are lucky since you can decorate the entire HttpClient with an IAsyncPolicy<HttpResponseMessage> policy.

You can do it via DI

IAsyncPolicy<HttpResponseMessage> policy = Policy<HttpResponseMessage>...
services.AddHttpClient("decoratedClient")
        .AddPolicyHandler(policy);

Or without DI as well

IAsyncPolicy<HttpResponseMessage> policy = Policy<HttpResponseMessage>...
var pollyHandler = new PolicyHttpMessageHandler(policy);
pollyHandler.InnerHandler = new HttpClientHandler();

var client = new HttpClient(pollyHandler);
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • 1
    Agreed! we can implement the resiliency policy at the HTTP Client level and this will ensure all request sent via same client will follow the same policy but I was looking for more generic approach where instead of implementing at HTTPClient level, can we do it to all HTTP request (protocol) even if its being sent from a HTTPClient who doesn't specify any policy. – Sunil Oct 05 '22 at 06:58