8 ways to improve ASP.net Web API performance

Source: Internet
Author: User
Tags http request json net serialization serialization


The asp.net Web API is a great technique. Writing Web APIs is so easy that many developers do not spend time on application structure design for good performance.



In this article, I'll introduce 8 techniques for improving the performance of the ASP.net Web API.



  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 transferred from the Json.NET serialization tool to the Servicestack.text 1.5.



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




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



  2 manually serialize JSON from DataReader



I have used this approach in my project and have gained 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 to objects, then take values from those objects and use JSON serializer to generate JSON.



Uses StringBuilder to generate JSON and returns stringcontent as the response in Webapi at the end.



var response = Request.createresponse (Httpstatuscode.ok); Response. Content = new Stringcontent (Jsonresult, Encoding.UTF8, "Application/json"); return response;



You can see more methods in Rick Strahl ' s blog



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



If you can give other message formats 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 the serialization of the protocol buffers is very fast, but also faster than JSON will format the results returned.



  4 to achieve compression



Use gzip or Deflate in your ASP.net Web API.



Compression is a simple and efficient way to reduce the size and responsiveness of response packs.



This is a very necessary feature that you can view more on the compressed article in my blog asp.net Web API GZip compression actionfilter with 8 lines of code.



  5 Using caching



The use of output caching in Web API methods is far-reaching. For example, if a large number of users visit the same day to change only one response (response) content.



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



  6 use typical ado.net as much as possible



The ado.net that you write manually is still the quickest way to get values from the database. If the performance of the Web API is really important to you, do not use orms.



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



Dapper and Hand-written fetch code quickly, and sure enough, all the ORM is slower than these three kinds.



The Llblgen with the resultset cache is fast, but it will iterate over the resultset and instantiate the object again in memory.



  7 implementing asynchronous methods in the Web API



The use of asynchronous Web API services dramatically increases the amount of Web API processing for HTTP requests.



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



[HttpGet] public async Task Operationasync () {await Task.delay (2000);}



  8 Returns a combination of multiple result sets and collections



Reducing the number of transmissions is not only good for multiple databases, but also for Web APIs, you can use the functionality of the result set.



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



Read the resultset var reader = command. ExecuteReader (); Read the data from this resultset while reader. Read ()) {suppliers. ADD (Populatesupplierfromidatareader (reader)); //Read the next ResultSet reader. NextResult (); Read the data from this second resultset while reader. Read ()) {products. ADD (Populateproductfromidatareader (reader)); }



You can return multiple objects in a single response to a Web API, and try to combine the multiple objects you return and return to the following:



public class Aggregateresult {public long maxid {get; set;} public list folders{get; set;} public list users{get; se T } }



This approach will reduce the HTTP request to your Web API.



Thank you for reading this article.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.