We recommend eight technologies to improve the performance of ASP. NET Web APIs, asp. netapi
In this article, I will introduce eight techniques to improve the performance of ASP. NET Web APIs.
1) Use the fastest JSON serialization Tool
JSON serialization has a critical impact on the performance of the entire ASP. NET Web API. In one of my projects, I switched from the JSON. NET serialization tool to ServiceStack. Text for a year and a half.
I have measured that the performance of Web APIs has increased by about 20%. I strongly recommend that you try this serialization tool. Here are some data on the performance of recent popular serialization tools.
Source: theburningmonk
Update: It seems that It seams that StackOverflow uses the fastest JSON serialization tool Jil they claim so far. For test data, see their GitHub page Jil serializer.
2) manually serialize JSON from DataReader
I have used this method in my projects and have received performance benefits.
You can manually create a JSON string from DataReader and avoid unnecessary object creation. In this way, you do not need to take values from DataReader and write objects, value from these objects and use JSON Serializer to generate JSON.
Use StringBuilder to generate JSON and return StringContent at the end as the response content in the WebAPI.
var response = Request.CreateResponse(HttpStatusCode.OK); response.Content = new StringContent(jsonResult, Encoding.UTF8, "application/json"); return response;
You can view more methods on Rick Strahl's blog
3) Try to use other protocol formats (protocol buffer, message pack)
If you can use other message formats in your project, such as Protocol Buffers or MessagePack, rather than JSON.
You can get a huge performance advantage, not only because Protocol Buffers is serialized very quickly, but also faster than formatting the returned results of JSON.
4) Compression
Use GZIP or Deflate in your ASP. NET Web API.
Compression is a simple and effective method to reduce the size and speed of response packets.
This is a very necessary function, you can view