I recently found out that using the Microsoft.Bcl.Async package to support async/await in Silverlight has a major downside – it rethrows exceptions without preserving the stack trace. That’s because Silverlight lacks ExceptionDispatchInfo, a mechanism that was added in .NET 4.5 that allows rethrowing exceptions while keeping their stack traces intact.

So I wrote this this gist to solve the problem:
https://gist.github.com/aelij/7d37fa3657921cbd9d3d

It works essentially the same as the original code from the async NuGet package, except for one thing – before rethrowing an async exception, it attempts to wrap it in a new exception of the same type, and place the old one as its inner exception. This way, try..catch blocks work as expected, and the stack trace is preserved.

Note that for C#’s method overload resolution to prefer this over the original implementation, you should place the AwaitExtensions2 class in your app’s root namespace. To verify that it does, you can call GetAwaiter and check the return type is TaskAwaiter2 (and not TaskAwaiter).

Enjoy!

Tagged with:
 

Comments are closed.

Set your Twitter account name in your settings to use the TwitterBar Section.