10 Performance-Improvement Tips for ASP.NET Core 3.0 Applications
Tags: webdev, productivity, dotnet
Published: 2020-02-07 10:03:03

---
title: 10 Performance-Improvement Tips for ASP.NET Core 3.0 Applications
published: true
date: 2020-02-06 12:00:29 UTC
tags: webdev, productivity, dotnet
canonical_url: https://www.syncfusion.com/blogs/post/10-performance-improvement-tips-for-asp-net-core-3-0-applications.aspx
cover_image: https://www.syncfusion.com/blogs/wp-content/uploads/2020/02/Tips-and-Tricks-to-improve-performance-of-ASP.NET-Core-3.0-672x372.jpg
---

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](https://en.wikipedia.org/wiki/ASP.NET_Core) applications by doing the following:

- [Avoiding synchronous calling and using asynchronous instead.](#avoid-synchronous-and-use-asynchronous)
- [Optimizing data access.](#optimize-data-access)
- [Using caching technology.](#use-caching-technology)
- [Using response caching middleware.](#use-response-caching-middleware)
- [Using JSON serialization.](#use-json-serialization)
- [Reducing HTTP requests.](#reduce-http-request)
- [Using exceptions only when necessary.](#use-exception-only-when-necessary)
- [Using response compression.](#user-response-compression)
- [Making **HttpContext** accessibility improvements.](#httpcontext-accessibility-improvements)
- [Making client-side improvements.](#client-side-improvements)

## 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.

```csharp
public class WebHost
{
    public virtual async Task StartAsync(CancellationToken cancellationToken = default)
    {
 
        // Fire IHostedService.Start
        await _hostedServiceExecutor.StartAsync(cancellationToken).ConfigureAwait(false);
 
        // More setup
        await Server.StartAsync(hostingApp, cancellationToken).ConfigureAwait(false);
 
        // Fire IApplicationLifetime.Started
        _applicationLifetime?.NotifyStarted();
 
        // Remaining setup
    }
}
```

[Entity Framework 3.0 Core](https://devblogs.microsoft.com/dotnet/announcing-ef-core-3-0-and-ef-6-3-general-availability/) also provides a set of async extension methods, similar to [LINQ](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/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()

```csharp
public async Task<List> GetBlogsAsync()
{
    using (var context = new BloggingContext())
    {
        return await context.Blogs.ToListAsync();
    }
}
```

### Asynchronous saving

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

```csharp
public static async Task AddBlogAsync(string url)
{
    using (var context = new BloggingContext())
    {
        var blogContent = new BlogContent { Url = url };
        context.Blogs.Add(blogContent);
        await context.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.
- Don’t try to get data that is not required in advance.
- Try touse [no-tracking queries](https://docs.microsoft.com/en-us/ef/core/querying/tracking#no-tracking-queries) in Entity Framework Core when accessing data for read-only purposes.
- Usefilter and aggregate LINQ queries (with  **.Where** ,  **.Select** , or  **.Sum**  statements), so filtering can be performed by the database.

You can find approaches that may improve performance of your high-scale apps in the [new features of EF Core 3.0](https://docs.microsoft.com/en-us/ef/core/what-is-new/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 the [_Microsoft.AspNetCore.ResponseCaching_](https://www.nuget.org/packages/Microsoft.AspNetCore.ResponseCaching/) package, which was implicitly added to ASP.NET Core.

In  **Startup.ConfigureServices** , add the Response Caching Middleware to the service collection.

```csharp
public void ConfigureServices(IServiceCollection services)
{
    services.AddResponseCaching();
    services.AddRazorPages();
}
```

## Use JSON serialization

ASP.NET Core 3.0 uses [**System.Text.Json**](https://docs.microsoft.com/dotnet/api/system.text.json) for 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.

- Don’t throw 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.

- Don’t compress natively compressed assets, such as PNG files.
- Don’t compress files with a size of 150-1,000 bytes.
- Don’t compress small files; it may produce a compressed file larger than the uncompressed file.

_( __source__ :_ [_https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AspNetCoreGuidance.md_](https://github.com/davidfowl/AspNetCoreDiagnosticScenarios/blob/master/AspNetCoreGuidance.md)_) _   
**Package: [Microsoft.AspNetCore.ResponseCompression](https://www.nuget.org/packages/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.

```csharp
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddResponseCompression();
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseResponseCompression();
    }
}
```

These are the providers:

- [Brotli Compression Provider](https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-3.0#brotli-compression-provider)
- [Gzip Compression Provider](https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-3.0#gzip-compression-provider)
- [Custom providers](https://docs.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-3.0#custom-providers)

```csharp
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddResponseCompression();
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseResponseCompression();
    }
}
```

## HttpContext accessibility improvements

**HttpContext** accessibility is only valid as long as there is an active HTTP request in ASP.NET Core. Here are some suggestions for accessing **HttpContext** from Microsoft’s documentation:

- [Don’t store IHttpContextAccessor.HttpContext in a field](https://docs.microsoft.com/en-us/aspnet/core/performance/performance-best-practices?view=aspnetcore-3.0#do-not-store-ihttpcontextaccessorhttpcontext-in-a-field).
- [Don’t access HttpContext from multiple threads](https://docs.microsoft.com/en-us/aspnet/core/performance/performance-best-practices?view=aspnetcore-3.0#do-not-access-httpcontext-from-multiple-threads).
- [Don’t use the HttpContext after the request is complete](https://docs.microsoft.com/en-us/aspnet/core/performance/performance-best-practices?view=aspnetcore-3.0#do-not-use-the-httpcontext-after-the-request-is-complete)
- [Don’t capture the HttpContext in background threads](https://docs.microsoft.com/en-us/aspnet/core/performance/performance-best-practices?view=aspnetcore-3.0#do-not-capture-the-httpcontext-in-background-threads)

## 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 won’t 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](https://www.syncfusion.com/aspnet-core-ui-controls) that are lightweight, modular, and responsive, including our [DataGrid](https://www.syncfusion.com/aspnet-core-ui-controls/grid), [Charts](https://www.syncfusion.com/aspnet-core-ui-controls/charts), and [Scheduler](https://www.syncfusion.com/aspnet-core-ui-controls/scheduler) controls. Feel free to try them and provide your feedback in the comments section.

You can also contact us through our [Support Forum](https://www.syncfusion.com/forums), [Direct-Trac](https://www.syncfusion.com/account/login), or [Feedback Portal](https://www.syncfusion.com/feedback). We are happy to assist you!

The post [10 Performance-Improvement Tips for ASP.NET Core 3.0 Applications](https://www.syncfusion.com/blogs/post/10-performance-improvement-tips-for-asp-net-core-3-0-applications.aspx) appeared first on [Syncfusion Blogs](https://www.syncfusion.com/blogs).