How do I convert .net 4.5 Async/Await example back to 4.0

前端 未结 4 987
故里飘歌
故里飘歌 2021-02-07 12:36

What would the equivalent asp.net mvc 4.0 code look like?

using System.Net;
using System.Net.Http;
using System.Web.Mvc;
using System.Threading.Tasks;
using Newt         


        
4条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 12:49

    NOTE: This is only for the Async/Await part.

    The easiest way would be to use the Async CTP. It has a Go live license which means you can use it in production code. You will have to make some modifications in cases where the Async CTP doesn't provide async equivalents.

    You can use Async CTP on Azure as the only thing required is an extra DLL. There is a similar SO question here Async CTP and Windows Azure Support

    I've been using Async CTP in production for over a year without problems. I'd definitely recommend this. Upgrading to .NET 4.5 is rather easy, essentially requiring only some class name changes (TaskEx to Task) and a few signature changes.

    If you can use Visual Studio 2012 RC you can also use the Async Targeting pack which will allow you to use your async code in .NET 4 with fewer changes than if you use the Async CTP.

    Finally, the brute-force solution is to use the TaskIterator from the ParallelExtensionsExtras samples to emulate what async/await does. You will have to convert all asynchronous invocations to tasks, or methods that return a task, and then iterate over the list of those tasks. It's a lot more code but it's the only solution if you don't want to use CTP code, even if it has a Go Live license.

    The ParallelExtensionsExtras include asynchronous tasks for the WebClient which may be useful if you decide to switch from HttpClient to WebClient.

提交回复
热议问题