A good solution for await in try/catch/finally?

You can move the logic outside of the catch block and rethrow the exception after, if needed, by using ExceptionDispatchInfo. static async Task f() { ExceptionDispatchInfo capturedException = null; try { await TaskThatFails(); } catch (MyException ex) { capturedException = ExceptionDispatchInfo.Capture(ex); } if (capturedException != null) { await ExceptionHandler(); capturedException.Throw(); } } This way, when … Read more

Site stopped working in asp.net System.Web.WebPages.Razor.Configuration.HostSection cannot be cast to

In the root Web.config make sure assemblyBinding contains the proper version for the assembly “System.Web.WebPages.Razor” and “System.Web.Mvc”. Check for their actual existence as well as my “System.Web.WebPages.Razor” assembly tag was missing causing the error. I had my assembly versions set to 3.0 and 5.0 respectively in the time of this writing with full NuGet updates. … Read more

An async/await example that causes a deadlock

Take a look at this example, Stephen has a clear answer for you: So this is what happens, starting with the top-level method (Button1_Click for UI / MyController.Get for ASP.NET): The top-level method calls GetJsonAsync (within the UI/ASP.NET context). GetJsonAsync starts the REST request by calling HttpClient.GetStringAsync (still within the context). GetStringAsync returns an uncompleted … Read more

Wrapping synchronous code into asynchronous call

It’s important to make a distinction between two different types of concurrency. Asynchronous concurrency is when you have multiple asynchronous operations in flight (and since each operation is asynchronous, none of them are actually using a thread). Parallel concurrency is when you have multiple threads each doing a separate operation. The first thing to do … Read more

Is it possible to “await yield return DoSomethingAsync()”

What you are describing can be accomplished with the Task.WhenAll method. Notice how the code turns into a simple one-liner. What happens is that each individual url begins downloading and then WhenAll is used combine those operations into a single Task which can be awaited. Task<IEnumerable<string>> DownLoadAllUrls(string[] urls) { return Task.WhenAll(from url in urls select … Read more

Using async-await on .net 4

Microsoft released the Async Targeting Pack (Microsoft.Bcl.Async) through Nuget as a replacement for the AsyncCTP. You can read more about it here: http://blogs.msdn.com/b/bclteam/archive/2013/04/17/microsoft-bcl-async-is-now-stable.aspx. You can read about the previous version here: http://blogs.msdn.com/b/lucian/archive/2012/04/24/async-targeting-pack.aspx. As this pack is officially supported, I now believe the best option for targeting XP + async would be using Visual Studio 2012 … Read more

How do I disable C# 6 Support in Visual Studio 2015?

You can set the language feature for each project separately by going to Properties => Build tab => Advanced button => Language Version and set your preferred version. You should realize that it will still use the new “C# 6.0” .Net Compiler Platform (codenamed Roslyn). However, that compiler will imitate the behavior of older compilers … Read more

How does Task become an int?

Does an implicit conversion occur between Task<> and int? Nope. This is just part of how async/await works. Any method declared as async has to have a return type of: void (avoid if possible) Task (no result beyond notification of completion/failure) Task<T> (for a logical result of type T in an async manner) The compiler … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)