Performance is the major factor for the success of any web application development. ASP.NET Core is one of the best platforms for high-performance web application development. In this article, I am going to share 5 tips that can help your web application perform better.
Performance is the major factor for the success of any web application development. ASP.NET Core is one of the best platforms for high-performance web application development. In this article, I am going to share 5 tips that can help your web application perform better.
The new features of C# language provide powerful features to implement asynchronous programming using keywords like async and await. Use of asynchronous programming will allows execution of the code in parallel and do not block the execution while performing heavy I/O operations while connecting to external data sources of web application.
Entity Framework Core comes up asynchronous extension methods that can be used to take advantage of asynchronous programming. You can find an asynchronous extension method available now for majority of the linq methods that you use. For example, using DbContext , you do database operations to get data from some table. ToListAsync() method is asynchronous variant of ToList() method for reading data asynchronously. Similarly, SaveChangesAsync() is asynchronous variant of SaveChanges() method is available to save data asynchronously.
Switching to asynchronous programming might require little bit of learning but you will get used to it.
Here is an example of the synchronous data access:
public List<Post> Posts() { return db.Posts.ToList(); }
Which can be re-written to:
public async Task<List<Post>> Posts() { return await db.Posts.ToListAsync(); }
Where db is an object of DbContext of your application.
Every application requires accessing data from the database. Since, data access operations directly impact performance of your web application, here we are sharing some of the most basic Entity Framework Core data access best practices that will require minor code adjustments to get major performance improvements.
Monitoring is the key operation in order to perform any performance improvement. This is yet another tip that is related to entity framework core and data access in your application where we will talk about how to efficiently setup entity frameowrk core to monitor sql, statistics and errors.
Enable logging – Starting Entity Framework Core 5.0, a new feature is introduced called simple logging. It supports feature which will logs sql statements generated against your LINQ queries and how much time the sql queries take to execute it.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.LogTo(Console.WriteLine);
There are additional configuration options like EnableSensitiveDataLogging and EnableDetailedErrors which helps review detailed sql, execution statistics and detailed errors.
Caching is another technique through which you can drastically improve your web application performance. Depending on the type of web application and data it uses, you can decide your cache strategy which includes:
There are cases when user request is processed with too many operations that can actually be switched to background in order to improve response time.
A good example is user registration request. A typical application will persist a new user to database, send out some kind of email for verification and then response is returned to the user. This process can be improved by switching the email sending operation to background processing and return response to end user early.
ASP.NET Core supports background operations and there are many other specialist frameworks available in ASP.NET Core like Quartz.Net and Hangfire which can be used for efficient background processing. There are other external frameworks like RabbitMq, Microsoft Msmq, Azure Service Bus, and AWS SQS which are all supports use case of processing data in background with efficient dashboards, scheduling, processing, monitoring and logging facilities.
Now you know at least 5 performance improvement tips for asp.net core web application that you can implement in your next web application.
Subscribe to Facile Technolab's monthly newsletter to receive updates on our latest news, offers, promotions, resources, source code, jobs and other exciting updates.