-
Notifications
You must be signed in to change notification settings - Fork 45
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
Try Catch aspect on async void method doesn't work #119
Comments
This is normal behavior. You're probably out of the advice before the method has ended. |
Agree that this is expected behaviour of Because method return So the question is how to use Mr.Advice to apply exception "swallowing" aspect on those methods (inject |
This is the reason why Mr.Advice gives a warning when weaving |
Things gets more complicated when we use async lambdas. public bool MyTest()
{
try
{
DoSomething("Here is a problem", async () =>
{
await Task.Delay(500);
throw new Exception("The exception tricky to catch!");
});
return true;
}
catch (Exception ex)
{
Console.WriteLine($"[E] {ex.GetType().Name}: {ex.Message}");
return false;
}
}
public void DoSomething(string args, Action callBack)
{
Console.WriteLine(args);
callBack();
} Despite you put all your code in |
Where is your advice applied? Could you post an archive with your test solution here? |
I'm not consider it as a bug, it just a way it is - there are exceptions we just can't handle in aspect. |
Tough one! The generated lamba is not a method, hence the non-detection. I tag this as a bug, even though I don't have any idea on how to fix this. |
OK, it is actually a method, but I was excluded from weaving because it is compiler-generated and caused problems on release build. Still a bug anyway... |
I don’t know how they do it, but actually it’s possible to write a Postsharp advice that is able to handle exceptions on async void methods. I’ve just tried and it worked. Maybe by injecting a .ContinueWith or something like that.... |
Hi @dougcunha, I didn't look at the generated code, however they probably change the code of the advised method in order to make it |
Also, they have a full team, paid by customers. On the other hand, I'm alone and work for free 😜 |
For now we can live with that by manual code reformatting (probably, once it can be done automatically): [ExcludePointcut] //or exclude all async voids on the level of assembly
async void X(pars)
{
await X_Wrapper(pars);
}
//MrAdvice is in full control here
async Task X_Wrapper(pars)
{
//implementation
} Lambdas also must be rewritten... |
Sure, I wasn't saying otherwise. Sorry if I sounded rude. My intention was just to point that maybe there's a way to solve this problem. I sincerely appreciate your work. All the best! |
I've been doing that, it's the best we can do now. Thank you! |
@dougcunha you missed the point in my answer; it was a smiling emoji. So no problem! |
@picrap Is there any recommended solution/workaround for this problem? exception in Async Method |
I replied ;) |
Hi, we have another problem with try/catch aspect described here
#118
It just doesn't come to catch block in case when unhandeled exception occurs in "async void" method:
with public async Task AddWhatever23() - all OK.
Any ideas of handling such case?
The text was updated successfully, but these errors were encountered: