Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the technologies to improve the performance of ASP.NET Web API

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "what are the technologies to improve the performance of ASP.NET Web API". In the daily operation, I believe that many people have doubts about the technology to improve the performance of ASP.NET Web API. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "what are the technologies to improve the performance of ASP.NET Web API?" Next, please follow the editor to study!

1) use the fastest JSON serialization tool

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

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

Source: theburningmonk

Update: it seems that It seams that StackOverflow uses what they claim to be the fastest JSON serialization tool to date, Jil. A test data can be found in their GitHub page Jil serializer.

2) serialize JSON manually from DataReader

I have used this approach in my project and have received 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 those objects and use JSON Serializer to generate JSON.

Use StringBuilder to generate 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

You can see more methods at Rick Strahl's blog.

3) use other protocol formats (protocol buffer, message pack) whenever possible

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

You will be able to gain a huge performance advantage, not only because Protocol Buffers serialization is very fast, but also faster than the result formatted by JSON.

4) implement compression

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

Compression is a simple and effective way to reduce the size and response speed of response packets.

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

5) use caching

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

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

6) use typical ADO.NET whenever possible

Manually written ADO.NET is still the quickest way to get values from a database. If the performance of Web API is really important to you, then don't use ORMs.

You can see the performance comparison between the most popular ORM.

Dapper and hand-written fetch code are fast, and sure enough, all ORM are slower than these three.

LLBLGen with the resultset cache is fast, but it has to traverse the resultset again and re-instantiate the object in memory.

7) implement asynchronous method in Web API

The use of asynchronous Web API services greatly increases the number of Http requests processed by Web API.

The implementation is simple, just use the async keyword and change the return type of your method to Task.

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

8) return the combination of multiple result sets and collections

Reducing the number of transfers is not only good for multiple databases, but also for Web API, you are likely to use the function of the result set.

That is, you can extract multiple result sets from DataReader. See the following demo code:

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

You can return multiple objects in a single Web API response. Try to combine your returned objects and return them as follows:

Class AggregateResult {public long MaxId {get; set;} public List Folders {get; set;} public List Users {get; set;}} at this point, the study of "what are the techniques to improve ASP.NET Web API performance" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report