【发布时间】:2016-09-22 10:27:07
【问题描述】:
我的代码是长时间运行的 i/o 绑定,非常适合 async/await 。我正在做存储库模式,无法弄清楚如何在控制器中等待,因为我在此代码上得到一个 对象不包含等待方法
public class HomeController : Controller
{
private ImainRepository _helper = null;
public HomeController()
{
this._helper = new MainRepository();
}
public async Task<string> Aboutt()
{
// Here I get the error
object main = await _helper.Top_Five() ?? null;
if(main != null)
{
return main.ToString();
}
else
{
return null;
}
}
}
我正在实施的方法可以正常工作,如下所示。我从数据库中获取数据并以字符串格式返回。我想要的是找到一种方法来制作 object main = await _helper.Top_Five() ?? null; 等待,否则我会将异步与同步代码混合使用。任何建议都会很棒...
public async Task<string> Top_Five()
{
try
{
using (NpgsqlConnection conn = new NpgsqlConnection("Config"))
{
conn.Open();
string Results = null;
NpgsqlCommand cmd = new NpgsqlCommand("select * from streams limit 15)t", conn);
using (var reader = await cmd.ExecuteReaderAsync())
{
while (reader.Read())
{
Results = reader.GetString(0);
}
return Results;
}
}
}
catch(Exception e)
{
// Log it here
return null;
}
}
【问题讨论】:
-
您的项目配置为使用哪个版本的 .NET 框架?很确定它必须 >= 4.5
-
是的,它实际上是 Asp.Net Core RC2
-
我无法重现该错误。你能提供一个minimal reproducible example吗?
标签: c# asp.net-mvc async-await asp.net-core .net-core-rc2