Migration von Polly nach microsoft.extensions.http.resilience - Erweiterung sollte HANDLE

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Migration von Polly nach microsoft.extensions.http.resilience - Erweiterung sollte HANDLE

by Anonymous » 14 Apr 2025, 09:43

Ich möchte von Polly auf microsoft.extensions.http.resilience addStandardResilienceHandler migrieren. Mein verkürzter Polly -Code ist der folgende: < /p>

Code: Select all

services.AddHttpClient()
.AddPolicyHandler((_, _) =>
{
return HttpPolicyExtensions.HandleTransientHttpError()
.Or(exception => exception.StatusCode == HttpStatusCode.Conflict)
.WaitAndRetryAsync(3, sleepDurationProvider: i => TimeSpan.FromSeconds(i * 2));
});
Der entscheidende Teil hier ist httppolicextensions.handletransientHttTerRor (). Oder (Exception => Ausnahme. Ich weiß wirklich, wie man dies in die neue Konfiguration umwandelt. Microsofts Standardimplementierung von SollHandle sieht so aus:

Code: Select all

public static readonly Func HandleOutcome = args => args.Outcome.Exception switch
{
OperationCanceledException => PredicateResult.False(),
Exception => PredicateResult.True(),
_ => PredicateResult.False()
};
< /code>
Meine aktuelle Konfiguration sieht so aus: < /p>
services.AddHttpClient()
.AddStandardResilienceHandler()
.Configure((options, _) =>
{
options.Retry.ShouldHandle = args =>
{
if (args.Outcome.Result?.StatusCode == HttpStatusCode.Conflict)
{
return PredicateResult.True();
}

return args.Outcome.Exception switch
{
OperationCanceledException => PredicateResult.False(),
not null => PredicateResult.True(),
_ => PredicateResult.False()
};
};
});
Ist dies der ersten Polly -Implementierung entspricht?

Top