.Net Core 2.1 was announced recently by Microsoft along with corresponding updates for ASP.Net Core and Entity Framework Core. You can check out Preview 1 by installing from the Getting Started page or by downloading the latest docker images from Docker Hub. And don’t let the minor version fool you, there are some exciting new things in this release.
Microsoft has issued numerous press releases and blog posts detailing all the new features in this version and it can be a little overwhelming. In this post I’ll try to cut through the noise and focus on my favorite new features. This is a subjective list and it’s far from exhaustive, so if you want to see the myriad of performance boosts, security enhancements, and new features you should check out the full release notes on Github. As with all .Net Core releases it is open source and cross platform, and it can be installed alongside existing versions of .Net Core. The Core team has done a good job supporting version targeting, making it easy to play around with the new version without disrupting existing applications that target older versions.
1. Global Tools
2. Span<T> and Memory<T>
These new types are designed to make it easier to design highly performant applications. Normally taking a slice of an array or other contiguous data structure requires copying the data into a new section of memory. These new types provide a virtual view of data with no time or space costs. In addition to the efficiency boosts, removing the necessity for data duplication can make your code less bug-prone. For example, imagine you have a 1,000,000 element array and you need to sort some internal subset of its elements. The old, unsafe way would require slicing, unnecessary memory allocation, and complex reinsertion. But with Span this can safely be done in just one step. Another, perhaps more common, use case is for read-only views of substrings. Strings are immutable in C# so any time you call Substrings() you’re allocating memory. With a Readonlyspan you get the same functionality faster and without wasting any additional memory! For more information about Span<T> check out Jared Parson’s introduction on Channel 9.
3. HTTPS by Default
Securing web apps with HTTPS is becoming the standard. It was common in the past to develop and run apps locally using HTTP, and to enable HTTPS only when the app was deployed to production. However, this can lead to a number of security issues that only surface in production. Developing locally with HTTPS makes it easier to catch these problems early on.
With this in mind ASP.Net Core will now install a development certificate automatically whenever a new project is created. The dotnet-dev-certs global tool (see the ‘Global Tools’ section above) makes it easy to add these dev certs to your trusted certificate store. With a trusted certificate you can begin developing with HTTPS in mind from day one. You can read more about it on the ASP.Net blog.
SignalR is the ASP.Net library for supporting real-time web applications. It has been a part of the ASP.Net Framework since 2013, and now it is available as a part of ASP.Net Core. It allows bi-directional communication from server and client via websockets when available, and other communication schemes for older browsers. With SignalR the server can push updates out to clients as new data becomes available, as opposed to the standard web model of the server waiting for clients to poll for data. The .Net Core version of SignalR represents a significant redesign, but most of the core APIs will be familiar to current users. You can see a good demo in the docs.
4. Lazy Loading
Entity Framework Core is finally getting lazy loading. A very useful feature that has existed in Entity Framework for years, its release has been pushed back by the EF team due to the fact that it can be bad for performance if not used carefully. Many people say lazy loading should be avoided entirely, and even those that encourage it urge caution. With lazy loading you can retrieve some entity from the datastore and later access related properties. The datastore will be queried dynamically for this related data as it is accessed. Without lazy loading you need to specify exactly what data you want upfront. Lazy loading can lead to problems because it’s easy to accidentally retrieve a lot more data than needed, or to retrieve data via many queries, instead of just one. This is a common issue known as the N+1 Problem. Despite these risks, the convenience it affords is hard to turn down.
This blog post is just scratching the surface of the updates in .Net Core 2.1. I encourage you to check out the release notes and see what else is in store. At the time of writing only the preview is out, so be sure to keep your eyes peeled for the release candidate!