[英]async task lacks await for MVC IHttpActionResult controller
I am using an async task to return Json data that I modeled using a data dictionary. 我正在使用一个异步任务来返回我使用数据字典建模的Json数据。 I want the task to execute asynchronously while it does my database calls in from my methods
report.GetActiveBuildDefinitions()
and my object of CurrentState which has a constructor which gets the data i need. 我希望任务在它从我的方法
report.GetActiveBuildDefinitions()
和我的CurrentState对象(它有一个构造函数可以获取我需要的数据report.GetActiveBuildDefinitions()
数据库调用时异步执行。 This is the constructor I am talking about -> new CurrentState(BuildDef.ID);
这是我正在谈论的构造函数->
new CurrentState(BuildDef.ID);
This controller functions properly. 该控制器正常运行。 But when i enable Warnings as Errors.
但是当我启用警告作为错误。 My class does not build.
我的课程无法建立。 It requires that I use the await keyword for my async task.
它要求我将await关键字用于异步任务。 I tried adding it to the return like
return await Json(state);
我尝试将其添加到return中,例如
return await Json(state);
but then it gives an error that "does not contain a definition for getawaiter" please help 但随后出现错误“不包含getawaiter的定义”,请帮助
This is my controller: 这是我的控制器:
[HttpGet]
public async Task<IHttpActionResult> GetBuildState()
{
try
{
List<CurrentState> state = new List<CurrentState>();
BuildReport report = new BuildReport();
List<BuildDefinition> definitions = report.GetActiveBuildDefinitions();
List<Dictionary<string, string>> buildInfo = new List<Dictionary<string, string>>();
foreach (BuildDefinition BuildDef in definitions)
{
state.Add(new CurrentState(BuildDef.ID));
}
foreach (CurrentState build in state)
{
Dictionary <string, string> entry = new Dictionary<string, string>();
if (build.ITestExists)
{
if (build.ITestState && build.BuildState)
{
entry.Add("BuildDef", build.BuildDefName);
entry.Add("GraphCode", "1");
entry.Add("WorkingBrokenTime", GetTime(build.BuildDefID));
entry.Add("Icon", "1");
entry.Add("Id", build.BuildDefID.ToString());
}
else if(build.BuildState && build.ITestState.Equals(false))
{
entry.Add("BuildDef", build.BuildDefName);
entry.Add("GraphCode", "2");
entry.Add("WorkingBrokenTime", GetTime(build.BuildDefID));
entry.Add("Icon", "0");
entry.Add("Id", build.BuildDefID.ToString());
}
else if (build.BuildState.Equals(false) && build.ITestState)
{
entry.Add("BuildDef", build.BuildDefName);
entry.Add("GraphCode", "3");
entry.Add("WorkingBrokenTime", GetTime(build.BuildDefID));
entry.Add("Icon", "0");
entry.Add("Id", build.BuildDefID.ToString());
}
else if (build.ITestState.Equals(false) && build.BuildState.Equals(false))
{
entry.Add("BuildDef", build.BuildDefName);
entry.Add("GraphCode", "0");
entry.Add("WorkingBrokenTime", GetTime(build.BuildDefID));
entry.Add("Icon", "0");
entry.Add("Id", build.BuildDefID.ToString());
}
}
else
{
if (build.BuildState)
{
entry.Add("BuildDef", build.BuildDefName);
entry.Add("GraphCode", "4");
entry.Add("WorkingBrokenTime", GetTime(build.BuildDefID));
entry.Add("Icon", "1");
entry.Add("Id", build.BuildDefID.ToString());
}
else
{
entry.Add("BuildDef", build.BuildDefName);
entry.Add("GraphCode", "5");
entry.Add("WorkingBrokenTime", GetTime(build.BuildDefID));
entry.Add("Icon", "0");
entry.Add("Id", build.BuildDefID.ToString());
}
}
buildInfo.Add(entry);
}
return Json(state);
}
catch (Exception)
{
return BadRequest(ModelState);
}
}
I want the task to execute asynchronously
我希望任务异步执行
But why? 但为什么? What benefit do you think asynchrony will get you, here?
您认为异步会给您带来什么好处?
Hint: async
does not mean "return early". 提示:
async
并不意味着“提前返回”。
Everything currently in your controller action is synchronous, so your controller action should be synchronous, too: 当前控制器动作中的所有内容都是同步的,因此您的控制器动作也应该是同步的:
public IHttpActionResult GetBuildState()
Now, if you change your I/O calls to be naturally asynchronous (ie, using EF6 asynchronous queries), then your controller action may end up calling an asynchronous method: 现在,如果将您的I / O调用更改为自然异步(即使用EF6异步查询),那么您的控制器操作可能最终会调用异步方法:
var definitions = await report.GetActiveBuildDefinitionsAsync();
and at that point, the compiler will tell you to make GetBuildState
asynchronous and change its return type to Task<IHttpActionResult>
. 然后,编译器将告诉您使
GetBuildState
异步并将其返回类型更改为Task<IHttpActionResult>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.