I am using an async task to return Json data that I modeled using a data dictionary. 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. This is the constructor I am talking about -> 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. I tried adding it to the return like return await Json(state);
but then it gives an error that "does not contain a definition for getawaiter" please help
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".
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:
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>
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.