小编arp*_*arp的帖子

有效地使用ASP.NET Web API的async/await

我试图async/await在我的Web API项目中使用ASP.NET 的功能.我不确定它是否会对我的Web API服务的性能产生任何影响.请在下面找到我的应用程序中的工作流程和示例代码.

工作流程:

UI应用程序→Web API端点(控制器)→Web API服务层中的调用方法→调用另一个外部Web服务.(这里我们有数据库交互等)

控制器:

public async Task<IHttpActionResult> GetCountries()
{
    var allCountrys = await CountryDataService.ReturnAllCountries();

    if (allCountrys.Success)
    {
        return Ok(allCountrys.Domain);
    }

    return InternalServerError();
}
Run Code Online (Sandbox Code Playgroud)

服务层:

public Task<BackOfficeResponse<List<Country>>> ReturnAllCountries()
{
    var response = _service.Process<List<Country>>(BackOfficeEndpoint.CountryEndpoint, "returnCountries");

    return Task.FromResult(response);
}
Run Code Online (Sandbox Code Playgroud)

我测试了上面的代码并且正在运行.但我不确定它是否正确用法async/await.请分享你的想法.

c# asp.net-mvc async-await asp.net-web-api

104
推荐指数
2
解决办法
15万
查看次数

标签 统计

asp.net-mvc ×1

asp.net-web-api ×1

async-await ×1

c# ×1