DbConnection.Open()起作用,但是dbConnection.OpenAsync()不能起作用 [英] DbConnection.Open() works but dbConnection.OpenAsync() doesn't

查看:24
本文介绍了DbConnection.Open()起作用,但是dbConnection.OpenAsync()不能起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个艰难的过程.使用完全相同的查询字符串,完全相同的以下代码:

This is a tough one. Using the exact same query string, the exact same following code:

using (var db = new SqlConnection(queryString))
{
   await db.OpenAsync();
   var results = await db.ExecuteSomethingAsync...;
   db.Close();
{

从Windows应用程序运行时可以正常工作.但是,当从IIS Express或IIS 7运行时,它将永远卡在 await OpenAsync()中.如果我用 db.Open()替换该行,则可以正常工作.有什么建议吗?

Will work when ran from a windows application. It will however get stuck forever in await OpenAsync() when ran from IIS Express or IIS 7. If I replace that line with db.Open() it works though. Any suggestions?

推荐答案

正如其他人所提到的,首先确保您没有 Wait Result 调用您的层次结构.一系列 async 方法终止于依赖于框架的入口点.在UI/WebForms应用中,这通常是一个 async void 事件处理程序.在WebAPI/MVC应用中,这通常是 async 操作.

As others have mentioned, first ensure you have no Wait or Result calls up your hierarchy. A chain of async methods ends at an entry point that depends on the framework. In a UI/WebForms app, this is usually an async void event handler. In a WebAPI/MVC app, this is usually an async action.

要检查ASP.NET的其他两件事是:

Two other things to check for ASP.NET are:

  • 确保您的目标平台是.NET 4.5.
  • 确保已将 UseTaskFriendlySynchronizationContext 设置为 true .

如果需要在多个平台上的共享库中支持 async ,则可以找到 Microsoft.Bcl.Async NuGet库有帮助.

If you need to support async in a shared library on multiple platforms, you may find the Microsoft.Bcl.Async NuGet library to be helpful.

这篇关于DbConnection.Open()起作用,但是dbConnection.OpenAsync()不能起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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