Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 7, 2021 04:41 am GMT

10 Performance-Improvement Tips for ASP.NET Core 3.0 Applications

Performance is very important; it is a major factor for the success of any web application. ASP.NET Core 3.0 includes several enhancements that scale back memory usage and improve turnout. In this blog post, I provide 10 tips to help you improve the performance of ASP.NET Core 3.0 applications by doing the following:

Avoid synchronous and use asynchronous

Try to avoid synchronous calling when developing ASP.NET Core 3.0 applications. Synchronous calling blocks the next execution until the current execution is completed. While fetching data from an API or performing operations like I/O operations or independent calling, execute the call in an asynchronous manner.

Avoid using Task.Wait and Task.Result, and try to use await. The following code shows how to do this.

0102030405060708091011121314151617

publicclassWebHost{publicvirtualasyncTask StartAsync(CancellationToken cancellationToken = default){// Fire IHostedService.Startawait_hostedServiceExecutor.StartAsync(cancellationToken).ConfigureAwait(false);// More setupawaitServer.StartAsync(hostingApp, cancellationToken).ConfigureAwait(false);// Fire IApplicationLifetime.Started_applicationLifetime?.NotifyStarted();// Remaining setup}}

Entity Framework 3.0 Core also provides a set of async extension methods, similar to LINQ methods, that execute a query and return results.

Asynchronous querying

Asynchronous queries avoid blocking a thread while the query is executed in the database. Async queries are important for quick, responsive client applications.

Examples:

  • ToListAsync()
  • ToArrayAsync()
  • SingleAsync()

1234567

publicasyncTask<List> GetBlogsAsync(){using(varcontext = newBloggingContext()){returnawaitcontext.Blogs.ToListAsync();}}

Asynchronous saving

Asynchronous saving avoids a thread block while changes are written to the database. It providesDbContext.SaveChangesAsync()as an asynchronous alternative toDbContext.SaveChanges().

123456789

publicstaticasyncTask AddBlogAsync(stringurl){using(varcontext = newBloggingContext()){varblogContent = newBlogContent { Url = url };context.Blogs.Add(blogContent);awaitcontext.SaveChangesAsync();}}

Optimize data access

Improve the performance of an application by optimizing its data access logic. Most applications are totally dependent on a database. They have to fetch data from the database, process the data, and then display it. If it is time-consuming, then the application will take much more time to load.

Recommendations:

  • Call all data access APIs asynchronously.
  • Dont try to get data that is not required in advance.
  • Try to useno-tracking queriesin Entity Framework Core when accessing data for read-only purposes.
  • Use filter and aggregate LINQ queries (with.Where,.Select, or.Sumstatements), so filtering can be performed by the database.

You can findapproaches that may improve performance of your high-scale apps in the new features of EF Core 3.0.

Use caching technology

Increase the performance of an application by reducing the number of requests to the server. Avoid calling the server every time and cache the data instead. Store the response for the future, and use it the next time you make a call for the same response.

These are some caching techniques:

  • In-memory caching.
  • Distributed cache.
  • Cache tag helper.
  • Distributed cache tag helper.

Use response caching middleware

Middleware controls when responses are cacheable. It stores responses and serves them from the cache. It is available in theMicrosoft.AspNetCore.ResponseCachingpackage, which was implicitly added to ASP.NET Core.

InStartup.ConfigureServices, add the Response Caching Middleware to the service collection.

12345

publicvoidConfigureServices(IServiceCollection services){services.AddResponseCaching();services.AddRazorPages();}

Use JSON serialization

ASP.NET Core 3.0 usesSystem.Text.Jsonfor JSON serialization by default. Now, you can read and write JSON asynchronously. This improves performance better than Newtonsoft.Json. The System.Text.Json namespace provides the following features for processing JSON:

  • High performance.
  • Low allocation.
  • Standards-compliant capabilities.
  • Serializing objects to JSON text and deserializing JSON text to objects.

Reduce HTTP requests

Reducing the number of HTTP requests is one of the major optimizations. Cache the webpages and avoid client-side redirects to reduce the number of connections made to the web server.

Use the following techniques to reduce the HTTP requests:

  1. Use minification.
  2. Use bundling.
  3. Use sprite images.

By reducing HTTP requests, these techniques help pages load faster.

Use exceptions only when necessary

Exceptions should be rare. Throwing and catching exceptions will consume more time relative to other code flow patterns.

  • Dontthrow and catch exceptions in normal program flow.
  • Use exceptions only when they are needed.

Use response compression

Response compression, which compresses the size of a file, is another factor in improving performance. In ASP.NET Core, response compression is available as a middleware component.

Usually, responses are not natively compressed. This typically includes CSS, JavaScript, HTML, XML, and JSON.

  • Dont compress natively compressed assets, such as PNG files.
  • Dont compress files smaller than about 150-1000 bytes

(source: https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AspNetCoreGuidance.md)

Package: Microsoft.AspNetCore.ResponseCompression is implicitly included in ASP.NET Core apps.

The following sample code shows how to enable Response Compression Middleware for the default MIME types and compression providers.

0102030405060708091011

publicclassStartup{publicvoidConfigureServices(IServiceCollection services){services.AddResponseCompression();}publicvoidConfigure(IApplicationBuilder app, IHostingEnvironment env){app.UseResponseCompression();}}

These are the providers:

010203040506070809101112

public
void
ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression(options =>
{
options.Providers.Add<BrotliCompressionProvider>();
options.Providers.Add<GzipCompressionProvider>();
options.Providers.Add<CustomCompressionProvider>();
options.MimeTypes =
ResponseCompressionDefaults.MimeTypes.Concat(
new
[] {
"image/svg+xml"
});
});
}




HttpContext accessibility improvements

HttpContext accessibility isonly valid as long as there is an active HTTP request in ASP.NET Core. Here are some suggestions for accessing HttpContext from Microsofts documentation:

Client-side improvements

Client-side optimization is one important aspect of improving performance. When creating a website using ASP.Net Core, consider the following tips:

Bundling

Bundling combines multiple files into a single file, reducing the number of server requests. You can use multiple individual bundles in a webpage.

Minification

Minification removes unnecessary characters from code without changing any functionality, also reducing file size. After applying minification, variable names are shortened to one character and comments and unnecessary whitespace are removed.

Loading JavaScript at last

Load JavaScript files at the end. If you do that, static content will show faster, so users wont have to wait to see the content.

Use a content delivery network

Use a content delivery network (CDN) to load static files such as images, JS, CSS, etc. This keeps your data close to your consumers, serving it from the nearest local server.

Conclusion

Now you know 10 tips to help improve the performance of ASP.NET Core 3.0 applications. I hope you can implement most of them in your development.

Syncfusion provides more than 70 high-performance ASP.NET Core UI controls that are lightweight, modular, and responsive, including our DataGrid, Charts, and Scheduler controls. Feel free to try them and provide your feedback in the comments section.

You can also contact us through our Support Forum, Direct-Trac, or Feedback Portal. We are happy to assist you!


Original Link: https://dev.to/techguy/10-performance-improvement-tips-for-asp-net-core-3-0-applications-24fa

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To