使用异步操作来运行同步code [英] Using an Async Action to Run Synchronous Code

查看:147
本文介绍了使用异步操作来运行同步code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有有两条路径,同步路径,只返回一个简单的观点,以及异步路径的搜索动作,即进行搜索异步,然后返回一个视图。两者都是GET请求,所以它们是相同的动作的一部分。

问题是,当我访问行动本地主机:XXXX /主页/搜索,页面只是无限加载。使用招,我可以看到,请求从不返回。我已经调试它,它它对code的最后一行,但同样,请求无法完成。

我已经简化了摄制于以下内容:

 公共异步任务<&的ActionResult GT;搜索()
{
    返回查看();
}

VS11警告我,code将没有的await,这是很好的同步运行,但请求无法完成。

如若这项工作?或者,我需要人在这里做什么?

修改

这是MVC 4针对.NET 4.5。

编辑2

对于那些谁更好看的东西在code,这就是为什么我需要同步的异步操作:

 公共异步任务<&的ActionResult GT;搜索(查询字符串= NULL)
{
    如果(string.IsNullOrWhiteSpace(查询))
        返回查看(新SearchViewModel()); //永远载入    VAR模型=等待_someService.SearchAsync(查询);
    返回查看(模型); //负荷
}


解决方案

这是在测试一个已知的bug。 <一href=\"http://social.msdn.microsoft.com/Forums/en-US/async/thread/558d9dfb-c6c4-4440-a755-4d8f2d99b71c\"相对=nofollow>引述斯蒂芬Toub:


  

简短的答案是,有在ASP.NET MVC中一个已知的bug在.NET 4.5 Beta版是导致这一问题时,异步方法同步完成。直到修正可用,一个简单的解决方法是增加等待Task.Yield();作为异步方法的第一行,迫使它异步完成。对于这个正常工作,你还需要确保你使用由ASP.NET在.NET 4.5中提供的新的SynchronizationContext,这意味着确保您有一行:

 &LT;添加键=ASPNET:UseTaskFriendlySynchronizationContextVALUE =真/&GT;

在您的配置文件的appSettings部分。


I have a search action that has two paths, a synchronous path, that just returns a simple view, and a asynchronous path, that does the search asynchronously and then returns a view. Both are GET requests, so they are part of the same action.

The problem is that when I access the action "localhost:XXXX/Home/Search", the page just infinitely loads. Using Fiddler, I can see that the request never returns. I've debugged it and it makes it to the last line of code, but, again, the request doesn't complete.

I've simplified the repro to the following:

public async Task<ActionResult> Search() 
{ 
    return View(); 
} 

VS11 warns me that the code will be run synchronously without an await, which is fine, but the request doesn't complete.

Should this work? Or do I need to do something else here?

Edit

This is MVC 4 targeting .NET 4.5.

Edit 2

For those who see things better in code, this is why I need synchronous in an async action:

public async Task<ActionResult> Search(string query = null)
{
    if (string.IsNullOrWhiteSpace(query))
        return View(new SearchViewModel());   // never loads

    var model = await _someService.SearchAsync(query);
    return View(model);    // loads
}

解决方案

This is a known bug in the beta. To quote Stephen Toub:

The short answer is that there is a known bug in ASP.NET MVC in the .NET 4.5 Beta that results in this issue when the async method completes synchronously. Until a fix is available, a simple workaround is to add "await Task.Yield();" as the first line of the async method, forcing it to complete asynchronously. For this to work correctly, you also need to ensure you're using the new SynchronizationContext supplied by ASP.NET in .NET 4.5, which means ensuring you have the line:

<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />

in the appSettings section of your configuration file.

这篇关于使用异步操作来运行同步code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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