Performance is one of the key characteristics to unveil a successful site. If your website takes longer than 3 seconds to load, chances are prospective customers might leave it and never return.
ASP.NET Core is a free, open-source, and cross-platform web development system developed by Microsoft. It is not an updated version of ASP.NET, but is entirely redesigned from scratch, and comes with a single programming model of ASP.NET MVC and ASP.NET Web API.
In this article, we will discuss a few best practices that improve ASP.NET Core performance.
Use the Latest Version
The maiden version of ASP.NET Core was released in 2016 with Visual Studio 2015. The currently available version is ASP.NET Core 3.0. If you look at the performance improvement from the principal version with the current variant, you will find applications created in ASP.NET Core 3.0 run faster compared to the past versions.
Microsoft always brings some performance improvements in subsequent versions.
Therefore, when building applications with ASP.NET Core, always choose the latest version of ASP.NET Core.
Avoid Synchronous Calls
An ASP.NET Core application must be designed so that multiple requests can be processed at the same time. Asynchronous APIs allow a small group of threads to handle thousands of simultaneous requests without waiting for calls to be blocked. Instead of waiting for a long sync task to run, threads can run on other requests.
A common performance issue in ASP.NET Core applications is call blocking, which can be asynchronous. Many synchronous blocking calls result in a lack of thread pooling and poor response times.
You can improve application performance by reducing the number of requests to the server at any time. This doesn’t mean you don’t call the server, it just means that you don’t make a call to the server every time.
The first time you call the server, the response received is stored. So, later when the server receives a call for a similar response, the data is first compared with the stored data for a match. If a match is found, the data is retrieved from the server instead of being called to the server.
Dealing with data where we can often receive it without calling the server is a good practice. By caching content, we can continue to reduce server calls and improve application performance.
These are some caching techniques:
- In-memory caching
- Distributed cache
- Cache tag helper
- Distributed cache tag helper
Optimize Data Access
You can also improve application output by optimizing its data access logic. Most of the applications depend entirely on the database. They need to fetch data from the database, process it, and then display it. If it’s time-consuming, the application can take a lot of time to load.
Here are some strategies you can use to improve app efficiency.
- Reduce the number of HTTP calls, i.e. the number of network round trips.
- Instead of making several calls to the server, aim to retrieve data in one or two calls.
- Set a cache for unchanged data.
- If not necessary, do not retrieve the data in advance. It raises the response load and thus slows down the application.
Optimize Custom Code
Apart from optimizing data access logic, we can also optimize business logic or custom middleware code. Optimizing or refactoring these codes lets us boost the efficiency of applications.
Here are a few points for doing that.
- Custom Logging, Authentication, or another code handler that runs on every request should be configured.
- Never carry out long-running custom execution in the business logic layer or middleware. It essentially blocks the request to go to the server, and the application takes a long time to get the results. You should optimize the code for this, either on the client-side or on the database side.
- Always check that a long-term task should be performed asynchronously without affecting other processes.
- You can take real-time client-server communication which works asynchronously.
Minimize Allocations in Large Objects
The .Net Core garbage collector simultaneously handles all allocations and releases the memory in the ASP. NET Core program. Cleaning unreferenced objects requires CPU time, thus, developers should also stop assigning unnecessary objects to hot code paths. Garbage collection is costly. Large heap generations such as generation 2 require a temporary suspension of program execution to clean up. Frequent allocation and clean-up will impair the output of the app.
A few recommendations are:
- Consider caching large objects that are often used. Caching large objects avoids costly allocations.
- Use the ArrayPool < T > to store large arrays for pool buffers.
- Don’t delegate many short-lived big objects to hot code paths.
Utilize JSON Serialization
.NET Core 3.0 uses System.Text.Json for JSON serialization. This means you can read and write JSON asynchronously without waiting for other processes to be executed. Use JSON to boost efficiency better than Newtonsoft. JSON. In addition, the System. Text. JSON namespace offers high performance, low allocation, compliant functionality, JSON text serialization objects, and deserialization of JSON text to objects.
Use Response Caching Middleware
Configure your code quickly using Middleware components that optimize frequently-called code paths and store responses and serve from the cache. The component can be found in Microsoft. AspNetCore. ResponseCaching package.
Response Compression for File Size
Response compression, which compresses the file size, is also another factor in improving performance. Response compression is available as a middleware component in ASP.NET Core.
Do not compress natively compressed properties, such as PNG files.
Don’t compress files that are smaller than 150-1000 bytes
Use Exceptions Judiciously
Exceptions are expected to be uncommon. Throwing and capturing exceptions can take more time than other code flow patterns.
Don’t throw and grab exceptions to the usual program flow.
Use exceptions only if they are required.
App diagnostic tools such as Application Insights could be used to recognize common exceptions in the app and understand how they function.
So that’s it! These tips will help you boost the performance of your ASP.NET Core application.
Brainvire is well equipped to provide robust and high-performing ASP.Net Core Application Development Services. With fast, stable, efficient, and scalable Microsoft .Net Development, we implement distributed web-based systems, individual software, and tools.