In this article, I will share about how to improve your ASP.NET Web API performance
1. Use fastest JSON serializer
JSON serialization can affect overall performance of ASP.NET Web API significantly. A year and a half I have switched from JSON.NET serializer on one of my project to ServiceStack.Text .
2. Use other formats if possible (protocol buffer, message pack)
If you can use other formats like Protocol Buffers or MessagePack in your project instead of JSON do it.
You will get huge performance benefits not only because Protocol Buffers serializer is faster, but because format is smaller than JSON which will result in smaller and faster responses.
3. Implement compression
Use GZIP or Deflate compression on your ASP.NET Web API.
Compression is an easy and effective way to reduce the size of packages and increase the speed.
4. Use Caching
If it makes sense, use output caching on your Web API methods. For example, if a lot of users accessing same response that will change maybe once a day.
5. Implement async on methods of Web API
Using asynchronous Web API services can increase the number of concurrent HTTP requests Web API can handle.
Implementation is simple. The operation is simply marked with the async keyword and the return type is changed to Task.
[HttpGet]
public async Task OperationAsync()
{
await Task.Delay(2000);
}
6. Use classic ADO.NET if possible
Hand coded ADO.NET is still the fastest way to get data from database. If the performance of Web API is really important for you, don’t use ORMs.
Please just see this table performance below
The Dapper and the hand-written fetch code are very fast, as expected, all ORMs are slower than those three.
LLBLGen with resultset caching is very fast, but it fetches the resultset once and then re-materializes the objects from memory.
Hope above tutorial help you.