【发布时间】:2015-07-05 04:56:36
【问题描述】:
我有一个非常基本的 MVC 控制器,只有一个动作:
public class HomeController : Controller
{
public ActionResult Index()
{
OpenConnection().Wait();
return View();
}
private async Task OpenConnection()
{
var synchronizationContext = SynchronizationContext.Current;
Debug.Assert(synchronizationContext != null);
using (
var connection =
new SqlConnection(
@"Data Source=(localdb)\ProjectsV12;Initial Catalog=Database1;Integrated Security=True;"))
{
await connection.OpenAsync(); // this always hangs up
}
}
}
问题是常规操作(不是异步版本)无法执行异步方法。在我的情况下,OpenConnection() 方法总是挂在 await connection.OpenAsync() 行。
一段时间后,我找到了两种使这段代码正常工作的方法。
-
使控制器的动作异步
public async Task<ActionResult> Index() { await OpenConnection(); return View(); } -
或者允许异步执行而不捕获原始 SychronizationContext - 为此:
await connection.OpenAsync();替换为:
await connection.OpenAsync().ConfigureAwait(false);
所以,我的猜测是我最初的问题是在 SynchronizationContext 附近的某个地方。但是 SynchronizationContext.Current 不为空,这让我怀疑我的猜测是否正确。
那么,谁能解释一下,为什么 MVC 控制器中的 not async 动作不能同步执行异步方法?
【问题讨论】:
标签: c# asp.net-mvc-4 async-await task