Polly WaitAndRetry with final exception does nothing
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I execute calls to an external service that is not very stable, and thus throws WebExceptions.
I want to retry a few times and after the last attempt I want to throw the last error received.
This is my attempt with Polly (v6.1.1):
public static Policy WaitAndRetryPolicy<T>(short nrOfRetryAttempts = 5) where T : Exception
{
var waitAndRetry = Policy
.Handle<T>()
.WaitAndRetry(nrOfRetryAttempts, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
var fallbackForLastError = Policy
.Handle<T>()
.Fallback(
fallbackAction: () => { },
onFallback: (ex) => { throw ex; });
return Policy.Wrap(fallbackForLastError, waitAndRetry);
}
Caller, legacy VB.Net:
Dim retryPolicy = Policies.WaitAndRetryPolicy(Of WebException)()
Dim theResult = retryPolicy.
ExecuteAndCapture(Function()
Return aProxy.GetSomething(a, b)
End Function).Result
When I run the code as depicted above, theResult stays null and it seems like the service is not called.
If I just use the WaitAndRetryPolicy without the Fallback function, the service is called and the retry mechanism works as expected (without throwing the exception of course).
How can I achieve my goal, without having to check PolicyResult.FinalException in the caller code?
c# vb.net polly
add a comment |
I execute calls to an external service that is not very stable, and thus throws WebExceptions.
I want to retry a few times and after the last attempt I want to throw the last error received.
This is my attempt with Polly (v6.1.1):
public static Policy WaitAndRetryPolicy<T>(short nrOfRetryAttempts = 5) where T : Exception
{
var waitAndRetry = Policy
.Handle<T>()
.WaitAndRetry(nrOfRetryAttempts, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
var fallbackForLastError = Policy
.Handle<T>()
.Fallback(
fallbackAction: () => { },
onFallback: (ex) => { throw ex; });
return Policy.Wrap(fallbackForLastError, waitAndRetry);
}
Caller, legacy VB.Net:
Dim retryPolicy = Policies.WaitAndRetryPolicy(Of WebException)()
Dim theResult = retryPolicy.
ExecuteAndCapture(Function()
Return aProxy.GetSomething(a, b)
End Function).Result
When I run the code as depicted above, theResult stays null and it seems like the service is not called.
If I just use the WaitAndRetryPolicy without the Fallback function, the service is called and the retry mechanism works as expected (without throwing the exception of course).
How can I achieve my goal, without having to check PolicyResult.FinalException in the caller code?
c# vb.net polly
What is the goal of theFallbackPolicy? That particular formulation will probably add nothing - it just rethrows the exception that was thrown on to it. Omitting that particular formulation, should have exactly the same effect. To have the last exception rethrown rather than captured intoPolicyResult.FinalException, simply useExecute(...)rather thanExecuteAndCapture(...)
– mountain traveller
Nov 23 '18 at 17:59
add a comment |
I execute calls to an external service that is not very stable, and thus throws WebExceptions.
I want to retry a few times and after the last attempt I want to throw the last error received.
This is my attempt with Polly (v6.1.1):
public static Policy WaitAndRetryPolicy<T>(short nrOfRetryAttempts = 5) where T : Exception
{
var waitAndRetry = Policy
.Handle<T>()
.WaitAndRetry(nrOfRetryAttempts, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
var fallbackForLastError = Policy
.Handle<T>()
.Fallback(
fallbackAction: () => { },
onFallback: (ex) => { throw ex; });
return Policy.Wrap(fallbackForLastError, waitAndRetry);
}
Caller, legacy VB.Net:
Dim retryPolicy = Policies.WaitAndRetryPolicy(Of WebException)()
Dim theResult = retryPolicy.
ExecuteAndCapture(Function()
Return aProxy.GetSomething(a, b)
End Function).Result
When I run the code as depicted above, theResult stays null and it seems like the service is not called.
If I just use the WaitAndRetryPolicy without the Fallback function, the service is called and the retry mechanism works as expected (without throwing the exception of course).
How can I achieve my goal, without having to check PolicyResult.FinalException in the caller code?
c# vb.net polly
I execute calls to an external service that is not very stable, and thus throws WebExceptions.
I want to retry a few times and after the last attempt I want to throw the last error received.
This is my attempt with Polly (v6.1.1):
public static Policy WaitAndRetryPolicy<T>(short nrOfRetryAttempts = 5) where T : Exception
{
var waitAndRetry = Policy
.Handle<T>()
.WaitAndRetry(nrOfRetryAttempts, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
var fallbackForLastError = Policy
.Handle<T>()
.Fallback(
fallbackAction: () => { },
onFallback: (ex) => { throw ex; });
return Policy.Wrap(fallbackForLastError, waitAndRetry);
}
Caller, legacy VB.Net:
Dim retryPolicy = Policies.WaitAndRetryPolicy(Of WebException)()
Dim theResult = retryPolicy.
ExecuteAndCapture(Function()
Return aProxy.GetSomething(a, b)
End Function).Result
When I run the code as depicted above, theResult stays null and it seems like the service is not called.
If I just use the WaitAndRetryPolicy without the Fallback function, the service is called and the retry mechanism works as expected (without throwing the exception of course).
How can I achieve my goal, without having to check PolicyResult.FinalException in the caller code?
c# vb.net polly
c# vb.net polly
asked Nov 23 '18 at 14:22
Michel van EngelenMichel van Engelen
2,06312038
2,06312038
What is the goal of theFallbackPolicy? That particular formulation will probably add nothing - it just rethrows the exception that was thrown on to it. Omitting that particular formulation, should have exactly the same effect. To have the last exception rethrown rather than captured intoPolicyResult.FinalException, simply useExecute(...)rather thanExecuteAndCapture(...)
– mountain traveller
Nov 23 '18 at 17:59
add a comment |
What is the goal of theFallbackPolicy? That particular formulation will probably add nothing - it just rethrows the exception that was thrown on to it. Omitting that particular formulation, should have exactly the same effect. To have the last exception rethrown rather than captured intoPolicyResult.FinalException, simply useExecute(...)rather thanExecuteAndCapture(...)
– mountain traveller
Nov 23 '18 at 17:59
What is the goal of the
FallbackPolicy? That particular formulation will probably add nothing - it just rethrows the exception that was thrown on to it. Omitting that particular formulation, should have exactly the same effect. To have the last exception rethrown rather than captured into PolicyResult.FinalException, simply use Execute(...) rather than ExecuteAndCapture(...)– mountain traveller
Nov 23 '18 at 17:59
What is the goal of the
FallbackPolicy? That particular formulation will probably add nothing - it just rethrows the exception that was thrown on to it. Omitting that particular formulation, should have exactly the same effect. To have the last exception rethrown rather than captured into PolicyResult.FinalException, simply use Execute(...) rather than ExecuteAndCapture(...)– mountain traveller
Nov 23 '18 at 17:59
add a comment |
2 Answers
2
active
oldest
votes
To have Polly rethrow any final exception, rather than capture it into PolicyResult.FinalException, simply execute the policy with the .Execute(...) or .ExecuteAsync(...) overloads, rather than .ExecuteAndCapture(...) or .ExecuteAndCaptureAsync(...)
No, it's that easy! First article I read led me to the wrong path. Execute does exactly what you describe, and what I need, thank you!
– Michel van Engelen
Nov 26 '18 at 6:57
add a comment |
I don´t know about the last exception but i have implemented a very similar behaviour with Retry and CircuitBreakerException(with Wrapping). So you can try 3 times and throw a circuitbreakerexception after 2 failures. Then you are able to react on the last exception.
Policy
.Handle<SomeExceptionType>()
.CircuitBreaker(2, TimeSpan.FromMinutes(x));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448422%2fpolly-waitandretry-with-final-exception-does-nothing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
To have Polly rethrow any final exception, rather than capture it into PolicyResult.FinalException, simply execute the policy with the .Execute(...) or .ExecuteAsync(...) overloads, rather than .ExecuteAndCapture(...) or .ExecuteAndCaptureAsync(...)
No, it's that easy! First article I read led me to the wrong path. Execute does exactly what you describe, and what I need, thank you!
– Michel van Engelen
Nov 26 '18 at 6:57
add a comment |
To have Polly rethrow any final exception, rather than capture it into PolicyResult.FinalException, simply execute the policy with the .Execute(...) or .ExecuteAsync(...) overloads, rather than .ExecuteAndCapture(...) or .ExecuteAndCaptureAsync(...)
No, it's that easy! First article I read led me to the wrong path. Execute does exactly what you describe, and what I need, thank you!
– Michel van Engelen
Nov 26 '18 at 6:57
add a comment |
To have Polly rethrow any final exception, rather than capture it into PolicyResult.FinalException, simply execute the policy with the .Execute(...) or .ExecuteAsync(...) overloads, rather than .ExecuteAndCapture(...) or .ExecuteAndCaptureAsync(...)
To have Polly rethrow any final exception, rather than capture it into PolicyResult.FinalException, simply execute the policy with the .Execute(...) or .ExecuteAsync(...) overloads, rather than .ExecuteAndCapture(...) or .ExecuteAndCaptureAsync(...)
answered Nov 24 '18 at 7:46
mountain travellermountain traveller
2,831819
2,831819
No, it's that easy! First article I read led me to the wrong path. Execute does exactly what you describe, and what I need, thank you!
– Michel van Engelen
Nov 26 '18 at 6:57
add a comment |
No, it's that easy! First article I read led me to the wrong path. Execute does exactly what you describe, and what I need, thank you!
– Michel van Engelen
Nov 26 '18 at 6:57
No, it's that easy! First article I read led me to the wrong path. Execute does exactly what you describe, and what I need, thank you!
– Michel van Engelen
Nov 26 '18 at 6:57
No, it's that easy! First article I read led me to the wrong path. Execute does exactly what you describe, and what I need, thank you!
– Michel van Engelen
Nov 26 '18 at 6:57
add a comment |
I don´t know about the last exception but i have implemented a very similar behaviour with Retry and CircuitBreakerException(with Wrapping). So you can try 3 times and throw a circuitbreakerexception after 2 failures. Then you are able to react on the last exception.
Policy
.Handle<SomeExceptionType>()
.CircuitBreaker(2, TimeSpan.FromMinutes(x));
add a comment |
I don´t know about the last exception but i have implemented a very similar behaviour with Retry and CircuitBreakerException(with Wrapping). So you can try 3 times and throw a circuitbreakerexception after 2 failures. Then you are able to react on the last exception.
Policy
.Handle<SomeExceptionType>()
.CircuitBreaker(2, TimeSpan.FromMinutes(x));
add a comment |
I don´t know about the last exception but i have implemented a very similar behaviour with Retry and CircuitBreakerException(with Wrapping). So you can try 3 times and throw a circuitbreakerexception after 2 failures. Then you are able to react on the last exception.
Policy
.Handle<SomeExceptionType>()
.CircuitBreaker(2, TimeSpan.FromMinutes(x));
I don´t know about the last exception but i have implemented a very similar behaviour with Retry and CircuitBreakerException(with Wrapping). So you can try 3 times and throw a circuitbreakerexception after 2 failures. Then you are able to react on the last exception.
Policy
.Handle<SomeExceptionType>()
.CircuitBreaker(2, TimeSpan.FromMinutes(x));
answered Nov 23 '18 at 16:48
RichyP7RichyP7
315
315
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448422%2fpolly-waitandretry-with-final-exception-does-nothing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the goal of the
FallbackPolicy? That particular formulation will probably add nothing - it just rethrows the exception that was thrown on to it. Omitting that particular formulation, should have exactly the same effect. To have the last exception rethrown rather than captured intoPolicyResult.FinalException, simply useExecute(...)rather thanExecuteAndCapture(...)– mountain traveller
Nov 23 '18 at 17:59