8 Ways to Improve ASP.NET Web API Performance

伊谢尔伦
Release: 2016-11-24 13:06:34
Original
1455 people have browsed it

 ASP.NET Web API is a great technology. Writing Web APIs is so easy that many developers don't spend time structuring their applications to achieve good performance.

 In this article, I will introduce 8 techniques to improve the performance of ASP.NET Web API.

 1) Use the fastest JSON serialization tool

JSON serialization has a critical impact on the performance of the entire ASP.NET Web API. On one of my projects, I switched from JSON.NET serialization tool to ServiceStack.Text for a year and a half.

 I have measured that the performance of Web API has increased by about 20%. I strongly recommend you to try this serialization tool. Here is some recent comparative data on the performance of popular serialization tools.

8 Ways to Improve ASP.NET Web API Performance

Source: theburningmonk

Update: It seems that It seems that StackOverflow is using Jil, which they claim is the fastest JSON serialization tool to date. A test data can be found on their GitHub page Jil serializer.

 2) Manual serialization of JSON from DataReader

I have used this method in my projects and obtained the performance benefits.

 You can manually create JSON strings from DataReader and avoid unnecessary object creation, so you don't have to take values ​​from DataReader and write objects, then take values ​​from these objects and use JSON Serializer to generate JSON.

Use StringBuilder to produce JSON and return StringContent at the end as the content of the response in WebAPI.

var response = Request.CreateResponse(HttpStatusCode.OK);
 
response.Content = new StringContent(jsonResult, Encoding.UTF8, "application/json");
 
return response;
Copy after login

You can check out more methods at Rick Strahl's blog

3) Use other protocol formats (protocol buffer, message pack) as much as possible

If you can use other message formats in your project, such as Protocol Buffers or MessagePack instead of using the JSON protocol format.

You will get huge performance advantages, not only because the serialization of Protocol Buffers is very fast, but also the formatting of the returned results is faster than JSON.

 4) Implement compression

 Use GZIP or Deflate in your ASP.NET Web API.

  Compression is a simple and effective way to reduce response packet size and response speed.

 This is a very necessary feature to use, you can view more articles about compression in my blog ASP.NET Web API GZip compression ActionFilter with 8 lines of code.

 5) Using caching

  in Web API The use of output caching in this method has profound implications. For example, if a large number of users access the same response content that only changes once a day.

  If you want to implement manual caching, such as caching user passwords in memory, please refer to my blog post Simple way to implement caching in ASP.NET Web API.

 6) Use typical ADO.NET as much as possible

  Manual Writing in ADO.NET is still the fastest way to get values ​​from a database. If Web API performance is really important to you, then don't use ORMs.

You can see a performance comparison between the most popular ORMs.

8 Ways to Improve ASP.NET Web API Performance

Dapper 和hand-written fetch code 很快,果不其然,所有的ORM都比这三种慢.

  带有resultset缓存的LLBLGen 很快,但它要重新遍历一遍resultset并重新再内存中实例化对象。

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

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

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

[HttpGet] 
public async Task OperationAsync() 
{  
    await Task.Delay(2000); 
}
Copy after login

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

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

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

// 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 ));
}
Copy after login

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

public class AggregateResult
{
     public long MaxId { get; set; }
     public List<Folder> Folders{ get; set; }
     public List<User>  Users{ get; set; }
}
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!