async await in .NET 4.0
i think you are agree that async await in c# 5 is a really cool feature. I will explain in the future blog post more detail about this. The problem now how if you want to use this feature in .NET 4.0. Let say that you can’t change the production environment to the latest .NET framework (4.5).
Firstly I have already installed Visual Studio 2012 in my machine. I try to change the target framework to .NET 4
At first I’m very happy because the async keyword is appear in my editor. But the red squiggly wipe all the happines.
You can see a cause of the problem at error window. Seems there are a missing type or assembly.
The answer to our problem is :
You can gain the simplicity and beauty of asynchronous to make your apps performance better.
Maybe you see there is no executable download in the previous link. That’s because you have to get the required library through Nuget.
Search “Microsoft.CompilerServices.AsyncTargetingPack” from nugget package manager. And install the package. That’s strange that the package is for Visual Studio 11. Well. Let’s try to give it a shoot.
After you install it the red squiggly disappear. You can dive into detail what’s contained in this package from object browser
There are a a lot of extensions method contain in AsyncCompatLibExtensions.
Then writes your codes.
Compile and run it asynchronously. If you see the IL with Diassembler you can see that our async await compiled into the same result as .NET 4.5. You should thanks the compiler baby.
Cheers