Some really easy ways to improve performance in your asp.net core web application are:

  1. response caching, which adds cache-related headers to responses, to reduce the number of requests a client makes to your web server;
  2. compressing response using gzip to reduce its size.

You may start with the default asp.net core web application, apply the above steps and check the result.

Setup Response Caching

Response caching adds cache-related headers to responses. These headers determine how you want the client, proxy and middleware to cache responses. Response caching can reduce the number of requests a client or proxy makes to the web server. Response caching can also reduce the amount of work the web server performs to generate the response.

An easy way to cache responses is through Cache Profiles, where they can be configured as options when setting up MVC in the `ConfigureServices` method in Startup.

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc(options =>
    {
        options.CacheProfiles.Add("Default",
            new CacheProfile()
            {
                Location = ResponseCacheLocation.Any,
                VaryByHeader = "User-Agent",
                Duration = 120
            });
    });
}

Then you can use the ResponseCacheAttribute in a controller or in a specific action:

[ResponseCache(CacheProfileName = "Default")]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    // ....
}

And the result in chrome dev tools will be:

For more details you can have a look at this article.

Setup Response Caching Middleware

Response caching middleware determines when responses are cacheable, while it also stores responses, and serves responses from cache. It depends on .NET Framework 4.5.1 or .NET Standard 1.3 or higher. This feature is available for apps that target ASP.NET Core 1.1.0 or higher.

Step 1. Install `Microsoft.AspNetCore.ResponseCaching` NuGet package

 

Step 2. Configure Services

In configure services section you may determine whether or not you want responses to be cached on case-sensitive paths through `UseCaseSensitivePaths`, and set the largest cacheable size for the response body in bytes through `MaximumBodySize`.

public void ConfigureServices(IServiceCollection services)
{
    services.AddResponseCaching(options =>
    {
        options.UseCaseSensitivePaths = true;
        options.MaximumBodySize = 1024;
    });
}

Step 3. Add middleware to the pipeline

Next, in configure section you may add the following line which will enable the response caching middleware when processing requests.

public void Configure(IApplicationBuilder app)
{
    app.UseResponseCaching();
}

Notes: 
Response caching middleware only caches 200 (OK) server responses, and for more details you can read here.

 

Setup GZip Compression

Setting up response compression in your asp.net core web application is very easy. You only need to follow these steps:

  • install `Microsoft.AspNetCore.ResponseCompression` NuGet package,
  • configure your services in `Startup.cs`, and
  • add the middleware in your pipeline.

This is quite useful when you want your app hosted without using `IIS` or `Nginx`, though it has some performance issues. Based on this analysis, the middleware is about 28% slower than the IIS compressions. Furthermore, it does not provide a mechanism to set a threshold to avoid compressing very small files.

Step 1. Install `Microsoft.AspNetCore.ResponseCompression` NuGet package

Step 2. Configure Services

services.Configure<GzipCompressionProviderOptions>(opts => opts.Level = System.IO.Compression.CompressionLevel.Optimal);
services.AddResponseCompression(opts =>
{
    opts.MimeTypes = new[]
    {
        // Default
        "text/plain",
        "text/css",
        "application/javascript",
        "text/html",
        "application/xml",
        "text/xml",
        "application/json",
        "text/json",

        // Custom
        "image/svg+xml",
    };
});

Step 3. Add the middleware to the pipeline

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseResponseCompression();

    //...
}

Now, using chrome dev tools you may see that all resources are gzipped.
(don’t forget the performance issues we talked about at the beginning)

 

 

 

Categorized in:

Tagged in:

,