Upgrade to ASP.NET Core 2.1 for Productivity and Performance Gains
Upgrading to ASP.NET Core 2.1 can deliver serious performance improvements to your web applications as well as make you much more productive as a developer.
I've been using .NET Core since it was released back in June 2016 as my development technology of choice for my personal projects, as well as helping introduce it as a mainstream technology choice at Just Eat (my current employer).
I find it so much more pleasurable to code against compared to "traditional" ASP.NET. With features such as self-hosting, built-in dependency injection and a high level of testability, you can really focus on solving the domain problem at hand, rather than worrying too much over boilerplate and ceremony.
Each new release, both major and minor, brings something new to geek-out over, but ASP.NET Core 2.1 has been a particular stand-out so far for new features and benefits that I find really compelling as a software developer.
In this blog post I'm going to cover two aspects of software development ASP.NET Core 2.1 really helps with — productivity and performance — with some specific examples from my personal side projects as well as professionally at my day job.
Below are links to the two GitHub Pull Requests to the repositories for my personal websites to upgrade them to ASP.NET Core 2.1:
- Update to ASP.NET Core 2.1 for martincostello.com
- Migrate to ASP.NET Core 2.1 for londontravel.martincostello.com
Developers like to be productive. Whether that's writing code faster, using less keystrokes, or automating tests. We'd all like to be coding and doing the parts of software development we enjoy, rather than the more mundane parts.
For me there's two new features in .NET Core 2.1 that really make the kind of software I generally work on, integrating applications with REST APIs, much easier to get on with than before. The first of these is HttpClientFactory.
HttpClientFactory provides a number of APIs that build on top of
HttpClient to make it easier to solve common cross-cutting concerns for HTTP API integrations, such as configuration, resiliency and scalability.
The dependency injection extension methods make it easier to setup named
HttpClient instances for your dependencies with appropriate configuration applied (base addresses, timeouts, request headers etc.). This can also be combined with typed clients, such as those provided by Refit, to let you abstract away a lot of the low-level interaction of HTTP APIs and get on with the business of writing integrations with your dependencies.
HttpClientFactory also ships an extensions library that allow you to easily integrate Polly into your HTTP requests to add behaviours such as retries for transient HTTP errors and circuit-breakers.
Another feature of HttpClientFactory is that it pools the underlying HTTP connections, meaning that system networking resources are managed for you, so your application gets the best performance it can by transparently re-using socket connections and handling things like DNS TTLs.
The builder configuration API it has also makes it easy to plug in additional behaviours, like HTTP request interception for use with integration tests.
Here's some links to GitHub Pull Requests where I've updated some of the applications I maintain to use HttpClientFactory:
As I mentioned above, HttpClientFactory makes it easier to integration test your applications, but ASP.NET Core 2.1 ships with another new feature that makes it even easier to integration test ASP.NET Core applications than in previous releases.
WebApplicationFactory<T>, which ships with the Microsoft.AspNetCore.Mvc.Testing NuGet package, provides a simple way to create a fixture to use in your integration tests that hosts your application in-memory. This allows you to integration test your MVC views and Razor pages without first deploying the application to a server like Kestrel or IIS.
You can even extend it to self-host your application on an HTTP or HTTPS port using Kestrel so you can run browser automation tests with technologies like Selenium.
For my London Travel Alexa skill's companion website, I combined both HttpClientFactory's ability to configure interception of HTTP requests with self-hosting with
WebApplicationFactory<T> to add browser integration tests for the OAuth identity integration for user accounts.
You can see the changes made to achieve this in this Pull Request. This PR increased the overall code coverage of the test suite by nearly 7%.
Developer productivity increases are great because they make it easier for you to get on with writing software. The benefit to your users is less tangible though, with the gains mainly being with accelerating your work so you can deliver value to users at an increased pace.
Performance improvements on the other hand are a win for both the developer of an application as well as its users. Your users get an improved experience with benefits such as faster page load times, but as an application developer and maintainer you get the benefit of more bang-for-your-buck from your hardware.
This can translate into real tangible benefits such as reduced hosting costs if you host in the cloud, such as in Microsoft Azure or Amazon Web Services, as you can serve more requests for the same level of service provisioning.
In practice this could mean that as your application's usage increases you can scale out later, or for a constant level of load you could maybe scale down and reduce your instance sizes. Either scenario could result in a reduction in your monthly hosting bills.
Microsoft and the .NET community have really, really put a focus into performance for ASP.NET Core 2.1. In fact, I'll let this tweet speak for itself from when I updated my website to ASP.NET Core 2.1 on the 31st of May after it was released.
Spot where I upgraded my website to #AspNetCore 2.1 yesterday: pic.twitter.com/m0rRgNL6G9— Martin Costello (@martin_costello) June 1, 2018
That's quite the cliff of a response time improvement isn't it?
I've been surprised by the amount of attention this quick tweet gathered, with it even popping up in the ASP.NET Community Standup and the results of round 16 the TechEmpower benchmarks.
Of course, improving the response times of the simple MVC application for my personal website isn't exactly a real-world example of an application under user load is it? The proof of the pudding is in the eating, so we updated two ASP.NET Core applications I maintain at work from 2.0 to 2.1 the week after, and the numbers involved there were just as impressive.
"So what about a real production application" I hear some of you ask? Well, here's some charts from UK production on Sunday evening for a @aspnet Core 2.1 web application and a 2.1 API application that it depends on.— Martin Costello (@martin_costello) June 11, 2018
TL;DR: Razor page rendering 43% faster, API 24% faster 🚀 pic.twitter.com/KubMSxnLLF
Yes, you read that correctly. It improved the performance of an MVC web application by 43%! Considering the changes were more-or-less just a framework upgrade (plus some new usage of HttpClientFactory), that's a tremendous improvement for the amount of development work put in to do the upgrade.
You can read more about the performance improvements in these two applications and how the metrics were collected in my post on the Just Eat Technology blog: ASP.NET Core 2.1 – Supercharging Our Applications 🚀
It was nice to see good feedback from members of the .NET community on the blog post as well, including from Ben Adams of Illyriad, who is quite a prolific contributor to .NET Core for performance gains.
Nice to see @justeat_tech and their customers (myself included) getting great performance benefits from #aspnetcore 2.1; because #perfmatters https://t.co/mz8gpekQYO pic.twitter.com/btT44mUW7x— Ben Adams (@ben_a_adams) June 19, 2018
Microsoft and the open source community have really delivered a great release with ASP.NET Core 2.1, with tonnes of new features and improvements, of which I've just touched the surface with a couple of my favourites.
Performance is the hero of .NET Core 2.1 though, so if you maintain any ASP.NET Core applications, I'd strongly recommend that you upgrade to 2.1 as soon as you can to really supercharge your applications and make yourself, and your users, happier for it.
Here's to ASP.NET Core 2.2 and beyond! 🚀