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 get good execution performance. In this article, I'll cover 8 techniques to improve the performance of ASP.NET Web APIs. 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 switched from the JSON.NET serializer to ServiceStack.Text for a year and a half. I've measured that the performance of the Web API has improved by about 20%. I highly recommend you try this serialization tool. Here is some recent comparison data on the performance of popular serialization tools.
SerializerPerformanceGraf Source: theburningmonk Update: It seems that It seams that StackOverflow uses Jil, their claimed JSON serialization tool to date. A test data can be found on their GitHub page Jil serializer. 2) Manually serialize JSON from DataReader I've used this approach in my projects and have received performance benefits. You can manually create JSON strings from the DataReader and avoid unnecessary object creation, so that you don't have to take values from the DataReader and write to objects, then take values from those objects and use the JSON Serializer to generate JSON. Use StringBuilder to generate JSON and return StringContent at the end as the content of the response in the WebAPI.
You can check out more ways to do this 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 MessagePacks, instead of using JSON. You will get a huge performance advantage, not only because Protocol Buffers are very fast in serialization, but also because they are formatted faster than JSON in the returned results. 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 responsiveness of response packets. This is a very necessary feature, you can check out more articles about compression in my blog ASP.NET Web API GZip compression ActionFilter with 8 lines of code. 5) Use caching Using output caching in the Web API method is profound. For example, if a large number of users access response content that changes only once a day. If you want to implement manual caching, such as caching user passwords to memory, see my blog post 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 the database. If the performance of your web APIs is really important to you, then don't use ORMs. You can see performance comparisons between the most popular ORMs. ORMMapper
Dapper and hand-written fetch code are fast, and sure enough, all ORMs are slower than the three. LLBLGen with a resultset cache is fast, but it has to re-iterate the resultset and instantiate the object in memory again. 7) Implement an asynchronous approach in the Web API Using an asynchronous Web API service dramatically increases the number of HTTP requests that Web APIs process. The implementation is simple, just use the async keyword and change the return value type of your method to Task.
8) Returns multiple result sets and combinations of sets Reducing the number of transfers is not only beneficial for multiple databases, but also for Web APIs, which allows you to use the power of result sets. This means that you can extract multiple result sets from DataReader, see the following demo code:
You can return multiple objects in a single response from a Web API, try combining your returned objects and return the following objects:
This will reduce HTTP requests to your WEB API. Thank you for reading this article.
|