Optimizing .Net Core Web API's

Tutorial: Building an ASP.NET Web API with ASP.NET Core | Toptal

To optimize your web API's first of all you have to set up your branch mark up to what extent you need to optimize and what are your goals.

There are several ways to optimize your API in .Net Core.

Asynchronous Programming

ASP.NET Core API's should be designed to process many requests simultaneously. Asynchronous APIs allow a small pool of threads to handle thousands of concurrent requests by not waiting on blocking calls. Rather than waiting on a long-running synchronous task to complete, the thread can work on another request. A common performance problem in ASP.NET Core API's is blocking calls that could be asynchronous. Many synchronous blocking calls lead to Thread Pool starvation and degraded response times.

Make repeatedly using function and heavy code area as asynchronous. Call data access, I/O, and long-running operations API's asynchronously if an asynchronous API is available for that data retrieval API. Do not use Task.Run to make a synchronous API asynchronous. Make controller/Razor Page actions asynchronous. Try to make the entire call stack to asynchronous in order to benefit from async/await patterns.

Response Compression

Response Compression is a native Asp.net Core middleware that is used for compressing the response. This process reduces the size of response which improves the response time because of small size response data.

From NuGet package manager download this package to utilize compression in your API project.

 Microsoft.AspNetCore.ResponseCompression

After adding the above package you can make using statements in API startup.cs file.

using Microsoft.AspNetCore.ResponseCompression;

using System.IO.Compression;

This will allow you call the different functions of the response compression library.


public void ConfigureServices(IServiceCollection services)
{
    services.AddResponseCompression();

    services.Configure<BrotliCompressionProviderOptions>(options =>
    {
        options.Level = CompressionLevel.Fastest;
    });
}


Network bandwidth is a limited resource. Reducing the size of the response usually increases the responsiveness of an app, often dramatically , above is one to way to reduce payload sizes by compress an app's responses.

Optimize Data Access

We can also improve the performance of the application by optimizing the data access logic. Most of the applications are totally dependent on the database and every time,All applications fetch data from DB and do some operation on it and show it to the client. If that process is not optimized then the application will be slow. Here are a few techniques that can be implemented while doing the code which can improve the performance of your application.

  1. Reduce the number of calls to your database, means you should always try to reduce the number of network round trips.
  2. Try to get all the data in one go, Means rather than making multiple calls to the DB server, just make one or two calls that can bring all the required data.
  3. Frequently used data should be cached.
  4. Don’t try to get the data in advance which is not required, it will increase the load on the response and your application will get load slower.


Comments

Popular posts from this blog

Setup source code of Visual studio on Bitbucket

.Net Core WebClient and proxy setup

Tables with ledger in sql server 2022