Technology

Async/Await best practices

Posted on

Here are some best practices on async/await programing model based on reading from: Async/Await – Best Practices in Asynchronous Programming TPL and async/await Best Practices for the Busy Developer Use ‘async Task’ or ‘async Task<T>’ instead of ‘async void’. Why? Primarily because of error-handling semantics. When using ‘async void’, any exception thrown will be raised directly on the SynchronizationContext that was active when the async void method started. When using ‘async Task’, exception is captured and plated on the Task object. The exception is asynchronous event handlers, which must regturn void (ex: ICommand.Execute implementations)

Async code works best if it goes all the way from bottom up (or top down). That means that you shouldn’t mix synchronous and asynchronous […]

Technology

Setting the Thread.CurrentPrincipal inside an async method

Posted on

Earlier this week I spent more time than I wanted for trying to figure out why I was losing the Thread.CurrentPrincipal value after having set it inside an async method. My history goes a lot like the one described here. But it took me sometime to find that website on Google, which was retrieving many results for web applications but very few for desktop. I also found similar problem described by this post. In summary, I was working on authentication for a desktop application, using Open ID Connect and Identity Server. My calls to the user store are all based on WCF services, and I wanted to have the authentication done asynchronously to prevent my UI from locking up. According to […]