ASP.Net Core is an open-source, fast, lightweight, and cross-platform rewrite of the original ASP.Net framework. It is a cross-platform solution that is compatible with Windows, Linux, and Mac OS X and optimized for performance. However, there are some best practices and strategies you can implement to make your .Net Core applications perform even better.
ASP.NET Code Performance Tips
In this programming tutorial, we will talk about how to improve the performance of ASP.NET 6 Core applications.
Avoid Blocking Calls: Use Asynchronous Calls
Blocking calls on synchronous can result in Thread Pool starvation and slow responses. It would be best if you took advantage of asynchronous calls to enable your applications to handle many requests simultaneously.
It is possible to handle thousands of concurrent requests using asynchronous APIs by not waiting for blocking calls to complete. Instead of waiting for a long-running synchronous activity to finish, the thread may focus on another request.
Use Inline Methods in C#
Inline methods are a feature of C# that allows programmers to write code in the same place where it is called. Inline methods can be used to improve performance in ASP.NET Core. They help improve performance because, as opposed to a regular method, inline methods do not have an overhead in terms of memory allocation, preserving method call information in the memory, etc.
Inlining methods improve efficiency by eliminating the need for passing arguments and saving and restoring registers. Note that a method with a throw statement will not be inlined by the JIT (just-in-time) compiler. Developers can, however, use a static helper method and include the throw statement in that method.
Avoid Using Virtual Methods
One common way to improve the performance of an ASP.NET 6 Core application is to minimize virtual calls. Virtual methods consume comparatively more resources than non-virtual methods. The reason is that virtual methods are mapped through a vtable (also known as a virtual table). This table contains information related to all virtual methods of all your classes.
Virtual methods can also be expensive in terms of performance, because they require the runtime to check the object’s type each time they are called. At runtime, the vtable is searched to find information related to the virtual methods. This consumes resources and you should avoid it unless there is a specific reason to use them. In essence, you should minimize the use of virtual methods to improve performance.
Reuse HttpClient Instances
Even after HttpClient connections have been closed, the socket remains open for a while. Therefore, if you attempt to generate new HttpClient instances each time an HttpClient is required, you will exhaust all of the sockets that are currently accessible.
It is highly recommended that developers dispose of HttpClient instances even if HttpClient implements the IDisposable interface. Instead, it would help if you reuse them.
To pool HttpClient instances, you can take advantage of the HttpClientFactory class. HttpClientFactory improves efficiency, scalability, and reliability by combining several connections into a single pool.
It would help if you refrained from directly creating and deleting HttpClient objects and instead used the HttpCLientFactory to get HttpClient instances.
Caching is one of the best and most popular techniques to boost performance. You should cache aggressively and cache any reasonably stale data. You can cache both web pages and data retrieved from the database. You can leverage response caching to cache responses.
Response caching refers to the capability of caching web server answers via cache-related headers in HTTP response objects. Response caching middleware is supported by ASP.Net Core and may be used to provide response caching.
Reduce Large Object Allocations
The garbage collector is adept at releasing memory occupied by objects in the managed heap. However, garbage collection can consume a lot of resources, especially if there are large objects (85 Kbytes or more). In other words, the cost of garbage collection for large objects is high.
To minimize large object allocations, you should take advantage of ArrayPool and cache large objects used frequently in your application. Programmers should not acquire locks on common code paths and avoid allocating too many short-lived objects on hot code paths.
Use Exceptions Only If Required
Exceptions are costly in terms of performance. Throwing and catching exceptions are detrimental to application’s performance and should be avoided as much as possible. As a result, exceptions should be used only when absolutely necessary. Developers should use exceptions sparingly in your application and avoid using exception handling to propagate logic flow.
Compress Static Content
Use Response Compression
Typically, decreasing the response size will increase the responsiveness of an application, often to a significant degree. Compressing an app’s responses is one method for reducing the overall size of payloads. In ASP.NET 6 Core, response compression has been made available as a middleware component.
When the response size is decreased, less data is sent back and forth between the server and the client, which in turn leads to an improvement in the application’s performance. When working with ASP.Net Core, you may use response compression to lower the response size and the associated bandwidth needs.
You can turn on response compression using the following code snippet in the Program.cs file:
var builder = WebApplication.CreateBuilder(args); builder.Services.AddResponseCompression(); var app = builder.Build(); app.UseResponseCompression(); app.MapGet("/", () => "Hello World!"); app.Run();
Optimize Data Access
You can optimize the data access code in your application to boost application performance. Keep in mind that data access is one of the most time-consuming processes in an application. It would be best if you used asynchronous data access approaches. You should retrieve only the data requested by the application. You can reduce data access calls by caching the frequently used data.
Final Thoughts on Application Performance in ASP.NET
In order to make your ASP.NET 6 Core application perform better, you can adopt several best practices. The priority of these tasks depends on your requirements, your available time and resources, etc. Programmers should also benchmark the application performance so that you can know the performance improvements after an optimization has been made.
Read more ASP.NET programming tutorials.