簡體   English   中英

在MVC 4中不使用Async / Await的含義

[英]The implications of not using Async/Await in MVC 4

我正在使用MVC 5構建Web應用程序,並且無法在PartialViewResults中使用Async / Await模式,因為它將導致運行時錯誤。 因此,我不等待某些任務。

例如,我有此標頭UI,它是PartialViewResult,可在我的APP的大多數頁面上使用:

        /// <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服務調用是獲取我通常使用GetAsync的MVC操作中的位置,並且在反序列化JSON結果時,也使用.GetAwaiter()。GetResult();

我的問題是,這會影響應用程序性能嗎? 因為它是MVC,並且沒有正在使用的UI線程(使用Razor渲染值),所以我認為我正在做的事情還可以,但是我很想知道其他人對此的看法。

缺口

如您所見,我的http服務調用是獲取我通常使用GetAsync的MVC操作中的位置,並且在反序列化JSON結果時,也使用.GetAwaiter()。GetResult();

就個人而言,我更喜歡在需要同步調用它們時使用同步API,而不是使用阻塞( GetAwaiter().GetResult() )。 阻塞方法假定您正在調用的異步方法對每個await使用ConfigureAwait(false) 如果他們忘記了一個,那么您就有潛在的僵局。

我的問題是,這會影響應用程序性能嗎?

從到達第一個字節的時間來看,它可能不會影響性能。 肯定會影響您的可擴展性; 如果您的Web服務器遭受重創或突然涌入請求,它將無法像使用純async解決方案那樣很好地擴展。

但是您對此無能為力。 這是ASP.NET classic(在ASP.NET Core中已修復)的局限性。 那就是生活。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM