设为首页收藏本站

LUPA开源社区

 找回密码
 注册
文章 帖子 博客
LUPA开源社区 首页 业界资讯 技术文摘 查看内容

8种提升ASP.NET Web API性能的方法

2014-7-31 08:44| 发布者: joejoe0332| 查看: 4528| 评论: 0|原作者: 土渣渣, 徐继开, mingshun|来自: oschina

摘要: SP.NET Web API 是非常棒的技术。编写 Web API 十分容易,以致于很多开发者没有在应用程序结构设计上花时间来获得很好的执行性能。在本文中,我将介绍8项提高 ASP.NET Web API 性能的技术。 ... ...


7)在 Web API 中实现异步方法

  使用异步的 Web API 服务大幅增加 Web API 对于Http 请求的处理数量。


  实现是简单的,只需使用 async  的关键字和 将你方法的返回值类型改为 Task 即可。

1
2
3
4
5
[HttpGet]  
public async Task OperationAsync()  
{   
    await Task.Delay(2000);  
}


8) 返回多个结果集和集合的组合


  减少传输的次数不仅多数据库有好处,对于 Web API同样 ,你才有可能使用结果集的功能。


  也就是说你可以从DataReader去提取多个结果集 参见以下演示代码:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// read the first resultset 
var reader = command.ExecuteReader(); 
  
// read the data from that resultset 
while (reader.Read()) 
    suppliers.Add(PopulateSupplierFromIDataReader( reader )); 
  
// read the next resultset 
reader.NextResult(); 
  
// read the data from that second resultset 
while (reader.Read()) 
    products.Add(PopulateProductFromIDataReader( reader )); 
}


  你可以在一个 Web API 的一次响应中返回多个对象,试着将你的返回的多个对象进行组合后返回 如下:


1
2
3
4
5
6
public class AggregateResult
{
     public long MaxId { getset; }
     public List Folders{ getset; }
     public List  Users{ getset; }
}


  这种方式将减少对你的WEB API的HTTP请求。


  感谢你读读这篇文章。


酷毙

雷人

鲜花

鸡蛋

漂亮
  • 快毕业了,没工作经验,
    找份工作好难啊?
    赶紧去人才芯片公司磨练吧!!

最新评论

关于LUPA|人才芯片工程|人才招聘|LUPA认证|LUPA教育|LUPA开源社区 ( 浙B2-20090187 浙公网安备 33010602006705号   

返回顶部