Six simple ways to improve ASP. NET Web API performance

Source: Internet
Author: User
Tags manual writing net serialization

The ASP. NET Web API is a great technology. It's so easy to write Web APIs that many developers don't spend time on application architecture design to get good execution performance.

In this article, I'll cover 8 techniques for improving the performance of ASP.

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 that the performance of the Web API has improved by around 20%. I strongly recommend that you try this serialization tool. Here are some of the most recent popular serialization tool performance comparison data.

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

2) manual serialization of JSON from DataReader

I have used this approach in my project and have gained the benefits in terms of performance.

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 produce JSON.

Use StringBuilder to generate JSON and return stringcontent at the end as the content that responds in Webapi.

var response = Request.createresponse (Httpstatuscode.ok);

Response. Content = new Stringcontent (Jsonresult, Encoding.UTF8, "Application/json");

return response;

You can see more ways at Rick Strahl ' s blog

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

If you can use other message formats in your project, such as Protocol buffers or messagepack instead of using JSON as a protocol format.

You will be able to give a huge performance advantage, not only because the serialization of the protocol buffers is very fast, but also more quickly than the JSON results formatted in return.

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 that you can view more about compressed articles in my blog ASP compression actionfilter with 8 lines of code.

5) Use caching

Using output caching in a Web API method is far-reaching. For example, if a large number of users access the same day only change the response (response) content.

If you want to implement manual caching, such as caching user passwords to memory, see my blog simple-to-implement caching in ASP. 6) Use typical ADO, as much as possible

The manual writing of ADO is still the quickest way to extract values from a database. If the performance of the Web API is really important to you, then don't use orms.

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

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

The Llblgen with the resultset cache is quick, but it is going to iterate over the resultset again and re-instantiate the object in memory.

Six simple ways to improve ASP. NET Web API performance

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.