Best Practices To Improve .NET Core Performance

  Solace  Infotech    January 31, 2022    492

 

Load time is one of the important characteristics of website or application performance as it unveils the success of a site. Hence, if a website/app is taking more than 3 seconds to load, customers/users might leave the site and never come back. Hence businesses prefer to have a perfect and robust web app. ASP.NET Core is the best technology for this. ASP.NET Core is an open-source, free, and cross-platform web development system that is created and handled by Microsoft. Though lots of developers are using it, it is necessary to know the best practices so as to improve the website/app performance. Hence here we came with some of the best practices. But before digging into it, let’s have an overview of ASP.NET Core.

What Is ASP.NET Core?

It is an open-source, lightweight, cross platform, fast and re-write of Asp.Net framework that runs on various operating systems like Windows, Linux, and Mac. Speed is one of the important feature of Asp.Net core so that it is optimized for better performance.

Also know- Top 15 .NET Core Libraries That You Must Know

Why Should You Use ASP.NET Core?

  • You can deploy app on cloud and on-premise cloud
  • Seamlessly works on OS such as Windows, Linux, Mac OS
  • You can build any web app and services, mobile backend services and IoT app development and 

So as to improve app performance, it is better to ensure that you build apps that use few amount of resources to generate desired outcomes. Let’s have a look at best practices to improve .Net core app performance. 

Top .NET Core Best Practices-

1. Optimize Data Access-

Optimizing data access logic of app is one of the most important best practices to improve the performance. Most of the apps are fully dependent on the database and they have to get data from the database, process it and display it. 

It is suggested that-

  • Do not get data that is necessary in advance
  • Call all data access through APIs asynchronously.
  • Use non-tracking queries, when retrieving data for read-only in Entity framework core.
  • Use aggregate and filter LINQ queries such as Where, Select or Sum statement, so that filter thing can be performed by database.

2. Use Cache-

Caching is a popular way to improve performance. You should cache to store data that is relatively stable. ASP.NET Core offers response caching middleware support that you can use to enforce response caching. You can use response caching to improve output caching and it can cache web server responses using cache-related headers to HTTP response objects. Also, caching large objects avoids costly allocations. Here are some of the caching techniques:

  • Distributing cache
  • In-memory caching
  • Distributed cache tag helper
  • Cache tag helper

Memory cache can be used or a distributed cache like NCache or Redis Cache can be used.

3. Enable Compression-

Reduction in response size can improve the performance app because it transfers less data between server and client. You can exploit response compression in ASP.Net Core to reduce requirements of bandwidth and lower the response. It acts as sure-shot middleware components in ASP.Net Core.  

public void ConfigureServices(IServiceCollection services_collection)
{
services_collection.AddResponseCompression();
services_collection.Configure<GzipCompressionProviderOptions>
(opt =>
{
opt.Level = CompressionLevel.Fastest;
});
}

4. Load Javascript From Bottom-

Except if they are required before, you should consistently strive to load our JS files at the end. As a result, your website will load faster and users won’t need to wait to see the information.

5. Use CDN(Content Delivery Network)-

If there are only numbered CSS and JS files then it is simple to load on the server. For bigger static files, you can think about using CDN. Most of the CDNs have numerous locations and serve files from local server. Website performance can be improved by loading files from a local server. 

6. Use Exceptions Only When Required-

Exceptions must be rare. Throw and catch exceptions are slow in comparison to other code flow patterns. Exceptions are not used to regulate the flow of program. Consider the logic of the program to identify and resolve exception-prone scenarios. Catch and throw exceptions for unusual or unexpected conditions. You can use tools like app insights to identify common exceptions in an app and how they perform. 

7. Routing-

You can provide detailed names and should use NOUNS rather than VERBS for routes/endpoints.

Don’t-

[Route("api/route- employee")]
public class EmployeeController : Controller
{
[HttpGet(“get-all-employee”)]
   public IActionResult GetAllEmployee() { }
[HttpGet("get- employee-by-Id/{id}"]
public IActionResult GetEmployeeById(int id) { }
}

Do:

[Route("api/employee")]
public class EmployeeController : Controller
{
[HttpGet]
public IActionResult GetAllEmployee() { }
[HttpGet("{id}"]
public IActionResult GetEmployeeById(int id) { }
}

8. Use Swagger-

Swagger is a representation of RESTful API that allows interactive documentation, discoverability and client SDK support. You just need a minute to setup Swagger tool. 

9. Perform Refactoring For Auto-generated Code-

There are many auto-generated codes, hence keep some time to examine the logic flow, and because you know your app better, you can improve it better.

Read more


 Article keywords:
.Net core performance, .net core best practices

 


 Share this article: 
Print Digg StumbleUpon del.icio.us Facebook Yahoo! Buzz Twitter Google Bookmarks LinkedIn MySpace Orkut PDF Scoopeo Viadeo Add to favorites
      

© Copyright - Articles XP