Wednesday, June 13, 2018

ASP.NET Web API : Steps To Improve ASP.NET Web API Performance

ASP.NET Web API is a .net framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

In this blog, we will discuss about 4 techniques to quickly improve the ASP.NET Web API performance.

1.     JSON serializer over XML:

JSON serializer use JSON Formatter and by default Web API provides media-type formatters for both JSON and XML.A media type formatter is used to read CLR objects from an HTTP message and write the CLR object in HTTP message body.
JSON serializer is highly recommend and it very faster compare to other Serlializer like XML.

2.     Asynchronous Task Based WebAPI :

     Async Web API action help to increase the number of concurrent HTTP requests Web API can handle. We can implement asynchronous programming for web api controller by using keywords async, Task, await

public class UserController : ApiController
{        public async Task<IHttpActionResult> Read()
{
var data = await _db.UserMaster.ToListAsync();
return Ok(data);
}
}

3.     Cache Data:
Web API  does not provide the output Caching attribute to cache web api response but you can Cache data in memory to process same future request without hitting database and it improve the performance of ASP.NET Web API and Microsoft provides the System.Runtime.Caching library for memory caching.  More information about web api caching

4.     Multi ResultSets:
Web API should return as many objects as you can in one Web API request and combining objects into one aggregate object and It reduce the number of HTTP request to web API

Example : Aggregate Object ‘UserScreenData’

class
 UserScreenData
{
public List<State> States { getset; }
public List<Country> Countries { getset; }
}

           Web API Method to return Aggregate object

public async Task<IHttpActionResult> GetScreenData()
{
UserScreenData screen = new Controllers.UserScreenData();
screen.State = await _db.State.ToListAsync();
screen.Countries = await _db.Country.ToListAsync();
return Ok(data);
}

Other ASP.NET WEB API related topics:
Thanks for visiting!!

3 comments:

rmouniak said...

Great post,Thanks for sharing
.Net Online Training Bangalore

SAM_Review said...

Hire dot net developers for all your web applications development, CMS customisations and migrating your existing apps to the .NET Platform seamlessly now.

M. said...

Great info. thnx
Is there a way to measure times on each step, or at least of some of them?

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...