我正在构建一个使用MVC 5的web应用程序,我不能在PartialViewResults中使用Async/Await模式,因为它会导致运行时错误。正因为如此,我不等待某些任务。在MVC中不使用异步/等待的影响4
例如,我有这个头UI这是在我的应用程序大多数页面使用的PartialViewResult:
/// <summary>
/// Get UI header for trainee's main details, name, aim, score etc
/// </summary>
/// <param name="personId"></param>
/// <param name="aim"></param>
/// <param name="showScore"></param>
/// <returns></returns>
public PartialViewResult _TraineeHeaderDetails(int personId, string aim, bool showScore, bool showEvidenceLink)
{
var result = _httpService
.Get(_urlConfigurations.GetTraineeDetails + personId + "/" + aim);
if (result.StatusCode != HttpStatusCode.OK)
//TODO: NG - add proper mvc error page redirect
throw new Exception("ID not found");
//RedirectToAction("")
// use GetAwaiter to get around async/await limitation in MVC 5
var model = _jsonDeserializer.GetModelAsync<TraineeHeaderViewModel>(result).GetAwaiter().GetResult();
if (model == null) return PartialView(new TraineeHeaderViewModel());
//For link to trainee summary
model.PersonId = personId;
model.AimCode = aim;
// flag to show/hide graph
model.ShowScore = showScore;
// flag to show/hide evidence link
model.ShowEvidenceLink = showEvidenceLink;
return PartialView(model);
}
正如你看到的,我的HTTP服务调用的就是其中MVC的行动我通常使用GetAsync和反序列化我的JSON结果时,我使用.GetAwaiter()。GetResult();
我的问题是,这会影响应用性能吗?因为它是MVC,并没有我正在使用的UI线程(值使用Razor渲染),所以我认为我正在做的事情没问题,但我很想知道别人怎么看待这个问题。
尼克
IIS服务器请求中存在有限的工作线程,使用async await将释放请求线程,直到异步操作完成 –