8 ways to improve ASP.NET Web API performance

Source: Internet
Author: User
Keywords Manual if can cache fastest
Tags .net aliyun api application asp asp.net blog cache

http://www.aliyun.com/zixun/aggregation/14156.html"> ASP .NET Web APIs are great technologies to write Web APIs so easily that many developers do not spend time designing application structures To get good execution performance.

In this article, I'll introduce eight technologies that improve the performance of ASP.NET Web APIs.

1) Use the fastest JSON serialization tool

Serialization of JSON has a key impact on the performance of the entire ASP.NET Web API. In one of my projects, I moved from JSON.NET serialization tool to ServiceStack.Text for a year and a half.

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

Update: It seems it seams that StackOverflow uses Jil, which they claim is by far the fastest JSON serialization tool. A test data can be found in their GitHub page Jil serializer.

2) Manually serialize JSON from the DataReader

I've used this method in my project and got the performance benefits.

You can manually create JSON strings from the DataReader and avoid unnecessary object creation, so you do not have to take values ​​from the DataReader and write to the objects, then take values ​​from these objects and generate JSON using the JSON Serializer.

Generate JSON using StringBuilder and return StringContent at the end as 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) 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 MessagePacks instead of using the JSON protocol format.

You will be able to get huge performance gains not only because Protocol Buffers are serialized very fast, but also faster than JSON formatting returned results.

4) to achieve compression

Use GZIP or Deflate in your ASP.NET Web API.

Compression is a simple and effective way to reduce response packet size and response speed.

This is a very necessary use of the function, you can view more articles on compression in my blog ASP.NET Web API GZip compression ActionFilter with 8 lines of code.

5) use caching

Using output caching in Web API methods is far-reaching, for example, if a large number of users access the same response that changes only once a day.

If you want to implement manual caching, such as caching user passwords in memory, see my blog Simple way to implement caching in the ASP.NET Web API.

6) Use typical ADO.NET whenever possible

Manually written ADO.NET is still the quickest way to get value from the database. If the performance of the Web API is really important to you, then do not use ORMs.

You can see the performance comparison between the most popular ORMs.

Dapper and hand-written fetch code quickly and sure enough, all ORMs are slower than all three.

LLBLGen with the resultset cache is fast, but it has to walk through the resultset again and re-instantiate the object in memory.

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.