如何转换.NET 4.5异步/等待例如回4.0 [英] How do I convert .net 4.5 Async/Await example back to 4.0

查看:127
本文介绍了如何转换.NET 4.5异步/等待例如回4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

会是什么equivallent asp.net的MVC 4.0 code样子?

 使用System.Net;
使用System.Net.Http;
使用System.Web.Mvc;
使用System.Threading.Tasks;
使用Newtonsoft.Json;
命名空间Web.Controllers
{
    公共类HomeController的:控制器
    {
        私人的HttpClient HttpClient的=新的HttpClient();
        私有静态动态拍摄;        公共异步任务<&的ActionResult GT;指数()
        {
            如果(张== NULL)
            {
                尝试
                {
                    VAR responseMessage =
                       等待httpClient.GetAsync
                            (http://api.dribbble.com/shots/everyone?per_page=30);
                    responseMessage.EnsureSuccessStatus code();                    VAR值=等待responseMessage.Content.ReadAsStringAsync();
                    镜头=等待JsonConvert.DeserializeObjectAsync<动态>(值);
                }
                赶上(引发WebException)
                {
                }
            }
            返回查看(张);
        }
    }
}


解决方案

注意:这是仅用于异步/恭候一部分。

最简单的方法是使用异步CTP 。它有一个去住的许可证,这意味着您可以在生产code使用它。你将不得不作出的情况下进行一些修改,其中异步CTP不提供异步当量。

您可以在Azure上使用异步CTP作为唯一需要的是一个额外的DLL。还有一个相似的,所以问题在这里异步CTP和Windows Azure的支持

我一直在使用异步CTP生产超过一年没有问题。我肯定会推荐这样做。升级到.NET 4.5是相当容易的,基本上只需要一些类名称的变化(TaskEx到任务)和几个签名的变化。

如果您可以使用Visual Studio 2012 RC,您还可以使用异步定位包,这将使你比如果使用异步CTP变化较少使用异步code在.NET 4中。

最后,蛮力解决方案是使用 TaskIterator 从ParallelExtensionsExtras样品效仿什么异步/等待呢。你将不得不对所有异步调用转换为任务或返回任务的方法,然后遍历这些任务的列表。这是一个很多code,但它是唯一的解决办法,如果你不希望使用CTP code,即使它有一个去住许可。

该ParallelExtensionsExtras包括这可能是WebClient的 异步任务如果你决定从HttpClient的切换到Web客户端非常有用。

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

using System.Net;
using System.Net.Http;
using System.Web.Mvc;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace Web.Controllers
{
    public class HomeController : Controller
    {
        private HttpClient httpClient = new HttpClient();
        private static dynamic shots;

        public async Task<ActionResult> Index()
        {
            if (shots == null)
            {
                try
                {
                    var responseMessage = 
                       await httpClient.GetAsync
                            ("http://api.dribbble.com/shots/everyone?per_page=30");
                    responseMessage.EnsureSuccessStatusCode();

                    var value = await responseMessage.Content.ReadAsStringAsync();
                    shots = await JsonConvert.DeserializeObjectAsync<dynamic>(value);
                }
                catch (WebException)
                {
                }
            }
            return View(shots);
        }
    }
}

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.

这篇关于如何转换.NET 4.5异步/等待例如回4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆