5 Performance Improvement Tips
Welcome to the third episode of Facile Weekly! In this week we are bringing you the 5 Performance Improvement Tips. Keep reading till the end to get some useful FREE PDF resources specially made for our subscribers only!
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.
1. Use asynchronous database operations with entity framework core.
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.
2. Follow Entity Framework Core data access best practices
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 and optimizing queries
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 framework 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.
领英推荐
4. Use caching
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:
?5. Switch to background processing
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 dashboads, scheduling, processing, monitoring and logging facilities.
?Conclusion
Now you know at least 5 performance improvement tips for asp.net core web application that you can implement in your next web application.
Earlier this week from Facile Technolab
FREE PDF for subscribers
Here is the first FREE PDF made with ? specially for our subscribers!
That's all for this week. See you next week!
Helping companies to go paperless by adopting digital transformation
1 年Prashant L. great work by you and your team.