-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why and When use ExecuteAsync or Execute? [what happens when you execute an async delegate through a sync policy] #483
Comments
@vany0114 You are seeing no difference in the specific example you posted because the delegates executed through the policy do not fault. Try this code and you'll see a difference. The key difference is that the executed delegate faults (but few enough times that the policy should handle it). Incidental (not significant) differences are:
I'll post a follow-up post in a moment explaining why class Program
{
static async Task Main(string[] args)
{
var toTestSync = new PollySyncAsyncTest();
var toTestAsync = new PollySyncAsyncTest();
await toTestSync.TestSync();
await toTestAsync.TestAsync();
}
}
class PollySyncAsyncTest
{
private int counter = 0;
public async Task TestAsync()
{
var policy = Policy
.Handle<Exception>()
.RetryAsync(3);
await policy.ExecuteAsync(async () =>
{
var something = await DoSomethingAsync();
if (something != true) throw new Exception("Assert fail");
});
}
public async Task TestSync()
{
var policy = Policy
.Handle<Exception>()
.Retry(3);
await policy.Execute( // Executing a Func<Task> delegate through a sync policy.
async () => // Providing the compiler an async delegate where it expects a sync delegate.
{
var something = await DoSomethingAsync();
if (something != true) throw new Exception("Assert fail");
});
}
private async Task<bool> DoSomethingAsync()
{
await Task.Delay(TimeSpan.FromSeconds(1));
if (++counter <= 2) throw new Exception("Fault which we might expect the policy to handle");
return true;
}
} |
To answer your original question:
Use async policies and ExecuteAsync(...) any time you are executing an async delegate. Why? In other words, why does At the line marked So at the line marked The final piece of understanding relies on knowing what
Does that help? EDIT: To look at this another way, we can look at why it works with the async policy and not with the sync policy.
In general the moral of the story is: don't mix sync and |
@reisenberger thanks for the great explanation! I just wanted to be aware in order to use them properly. |
@vany0114 np. Thanks for the great question! |
I had the same question and this is a great explanation @reisenberger. Should definitely be added to the wiki! |
This comment on #895 says "Polly v8 unifies sync and async policies into a single API concept, which should address this.". I don't have a link to any documentation, however. |
Hi guys,
I already read your article explaining synchronous vs asynchronous policies, and I understand your point, but in the end, it looks like there is no difference if I use either
ExecuteAsync
orExecute
method, in the below example I'm executing an async method with Sync and Async policies and it works ok in both cases, so what's the difference, why I would use one or another, I'm confused with the behavior.The text was updated successfully, but these errors were encountered: