8 ways to improve the performance of ASP. NET Web API

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.

Source: Theburningmonk

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.

7) Implementing asynchronous methods in the Web API

Using the asynchronous Web API service greatly increases the number of HTTP requests processed by the Web API.

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

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

8) Returns a combination of multiple result sets and collections

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

That means you can extract multiple result sets from DataReader see the following demo code:

Read the first Resultsetvar reader = command. ExecuteReader ();   Read the data from that Resultsetwhile (reader. Read ()) {    suppliers. ADD (Populatesupplierfromidatareader (reader));}   Read the next resultsetreader. NextResult ();   Read the data from that second resultsetwhile (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 have returned to return the following:

public class aggregateresult{public     long Maxid {get; set;}     Public list<folder> folders{get; set;}     Public list<user>  users{get; set;}}

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

  • 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.