Sunil
Sunil

Reputation: 17

Implement Resiliency at protocol level

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!!!

Upvotes: -1

Views: 246

Answers (1)

Peter Csala
Peter Csala

Reputation: 22829

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);

Upvotes: 0

Related Questions