.NET Core Performance Tuning - How to speed up asp.net core web application Part 2
Environment Settings for Development, Staging and Production
As we all know in all projects we have things like JS, CSS and other things that are very in different stages of development, so having proper setting for all these can help a lot and can save lot of time like we need to minify JS and CSS in production or we may load them from different paths. So it's always good to use of appsettings.Development.json file etc.
Use of Clean Controller as possible
Having a smaller code block in the Controller will help to solve the problem quickly as well as active fast transfer control while resolving and responding.
Avoid blocking calls
ASP.NET Core is best in processing many requests simultaneously.
- Do not Block asynchronous execution by calling Task.Wait or Task.Result.
- Do not acquire locks in common code paths. Because ASP.NET Core design is suitable to run code in parallel.
Things to do to avoid blocking is,
- Use asynchronous codes,
- Call APIs asynchronously for long-running,
- Use Asynchronous Controller actions,
Note: To have the benefit of Asynchronous entire code block also need to be Asynchronous.
Optimize Data Access When using LINQ queries
- Do filter and aggregate LINQ queries, so that the filtering is done by the database.
- for example, with .Where, .Select, or .Sum statements.
It is good to use pure ADO.NET for performance critical application to know more on it please visit How to use ADO.NET in .NET Core 2.x for Performance Critical Applications and A source project is in Github a link is SQLHelper With ADO.NET SQL Helper
Complete long-running Tasks outside of HTTP requests
- As the use of Azure Function, or similar services can help to complete long-running task outside of HTTP requests.
- Use of asynchronous services like SignalR is helpful for real-time communication.
Do proper use of HTML Helpers and Tag Helpers
Do the conditional works in HTML Helpers, do not do if-else logic in the view, and use Tag Helpers for HTML processing building HTML on logic as Tag Helper operation is done on stream it will be extremely faster and reduce a lot of duplicate codes too. How to use Tag Helper please visit How to Create Custom Tag Helper in ASP.NET Core
Think to Avoid Exceptions
- Do your logic to check & handle conditions which can cause an exception,
- Do throw or catch exceptions for unavoidable conditions,
- Find diagnostic tools (like Visual Studio Diagnostic Tool, PerfCollect, MiniProfiler, dotTrace and dotMemory from JetBrains, Application Insights, Application Map etc) to identify exception and may lead to performance degrading problems,
Use of Global Error Handling
As error always pain and we have to handle them, so in .NET core we have a better way to do this with adding proper middleware in Startup.cs file. More detail also can be found in https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-2.2
A simple configuration option for Global Error Handling is given below,
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseExceptionHandler(config =>
{
config.Run(async context =>
{
context.Response.StatusCode = 500;
context.Response.ContentType = "application/json";
var error = context.Features.Get<IExceptionHandlerFeature>();
if (error != null)
{
var ex = error.Error;
await context.Response.WriteAsync(new ErrorModel()
{
StatusCode = 500,
ErrorMessage = ex.Message
}.ToString();
}
});
});
app.UseMvc();
}
We can also create our own custom error handler middleware too. We also have the option to use Exception filters to know more on it please visit How to use Exception filters in ASP.NET Core MVC? , so check and think which is good for your application too.
Note:- There is always cost for handling errors so try to have a code which will not throw exceptions.
Try to use new ASP.NET Core releases
Every time new release come always have performance and security fixes like ASP.NET Core 2.2 have HTTP/2 support.
If you missed my first post on .NET Core Performance Tuning - How to speed up asp.net core web application, then please visit .NET Core Performance Tuning - How to speed up asp.net core web application.
Thank You, Happy .NET Core Performance Tuning :)