Effectively use async/await with ASP.NET Web API

前端 未结 4 1979
生来不讨喜
生来不讨喜 2020-11-30 16:40

I am trying to make use of the async/await feature of ASP.NET in my Web API project. I am not very sure whether it will make any difference in performance of my

4条回答
  •  鱼传尺愫
    2020-11-30 17:13

    I would change your service layer to:

    public Task>> ReturnAllCountries()
    {
        return Task.Run(() =>
        {
            return _service.Process>(BackOfficeEndpoint.CountryEndpoint, "returnCountries");
        }      
    }
    

    as you have it, you are still running your _service.Process call synchronously, and gaining very little or no benefit from awaiting it.

    With this approach, you are wrapping the potentially slow call in a Task, starting it, and returning it to be awaited. Now you get the benefit of awaiting the Task.

提交回复
热议问题