Home Programming High Performance ASP.NET Core High Performance

ASP.NET Core High Performance

0
2467
19 min read

In this article by James Singleton, the author of the book ASP.NET Core High Performance, we will see that many things have changed for version 2 of the ASP.NET Core framework and there have also been a lot of improvements to the various supporting technologies. Now is a great time to give it a try, as the code has stabilized and the pace of change has settled down a bit.

There were significant differences between the original release candidate and version 1 of ASP.NET Core and yet more alterations between version 1 and version 2. Some of these changes have been controversial, particularly around tooling but the scope of .NET Core has grown massively and ultimately this is a good thing.

Learn Programming & Development with a Packt Subscription

One of the highest profile differences between 1 and 2 is the change (and some would say regression) away from the new JavaScript Object Notation (JSON) based project format and back towards the Extensible Markup Language (XML) based .csproj format. However, it is a simplified and stripped down version compared to the format used in the original .NET Framework.

There has been a move towards standardization between the different .NET frameworks and .NET Core 2 has a much larger API surface as a result. The interface specification known as .NET Standard 2 covers the intersection between .NET Core, the .NET Framework, and Xamarin. There is also an effort to standardize Extensible Application Markup Language (XAML) into the XAML Standard that will work across Universal Windows Platform (UWP) and Xamarin.Forms apps.

C# and .NET can be used on a huge amount of platforms and in a large number of use cases, from server side web applications to mobile apps and even games using engines like Unity 3D.

In this article we will go over the changes between version 1 and version 2 of the new Core releases. We will also look at some new features of the C# language. There are many useful additions and a plethora of performance improvement too.

In this article we will cover:

  • .NET Core 2 scope increases
  • ASP.NET Core 2 additions
  • Performance improvements
  • .NET Standard 2
  • New C# 6 features
  • New C# 7 features
  • JavaScript considerations

New in Core 2

There are two different products in the Core family. The first is .NET Core, which is the low level framework providing basic libraries. This can be used to write console applications and it is also the foundation for higher level application frameworks.

The second is ASP.NET Core, which is a framework for building web applications that run on a server and service clients (usually web browsers). This was originally the only workload for .NET Core until it grew in scope to handle a more diverse range of scenarios.

We’ll cover the differences in the new versions separately for each of these frameworks. The changes in .NET Core will also apply to ASP.NET Core, unless you are running it on top of the .NET Framework version 4.

New in .NET Core 2

The main focus of .NET Core 2 is the huge increase in scope. There are more than double the number of APIs included and it supports .NET Standard 2 (covered later in this article). You can also refer .NET Framework assemblies with no recompile required. This should just work as long as the assemblies only use APIs that have been implemented in .NET Core.

This means that more NuGet packages will work with .NET Core. Finding if your favorite library was supported or not, was always a challenge with the previous version. The author set up a repository listing package compatibility to help with this. You can find the ASP.NET Core Library and Framework Support (ANCLAFS) list at github.com/jpsingleton/ANCLAFS and also via anclafs.com. If you want to make a change then please send a pull request. Hopefully in the future all packages will support Core and this list will no longer be required.

There is now support in Core for Visual Basic and for more Linux distributions. You can also perform live unit testing with Visual Studio 2017, much like the old NCrunch extension.

Performance improvements

Some of the more interesting changes for 2 are the performance improvements over the original .NET Framework. There have been tweaks to the implementations of many of the framework data structures. Some of the classes and methods that have seen speed improvements or memory reduction include:

  • List<T>
  • Queue<T>
  • SortedSet<T>
  • ConcurrentQueue<T>
  • Lazy<T>
  • Enumerable.Concat()
  • Enumerable.OrderBy()
  • Enumerable.ToList()
  • Enumerable.ToArray()
  • DeflateStream
  • SHA256
  • BigInteger
  • BinaryFormatter
  • Regex
  • WebUtility.UrlDecode()
  • Encoding.UTF8.GetBytes()
  • Enum.Parse()
  • DateTime.ToString()
  • String.IndexOf()
  • String.StartsWith()
  • FileStream
  • Socket
  • NetworkStream
  • SslStream
  • ThreadPool
  • SpinLock

We won’t go into specific benchmarks here because benchmarking is hard and the improvements you see will clearly depend on your usage. The thing to take away is that lots of work has been done to increase the performance of .NET Core, both over the previous version 1 and .NET Framework 4.7. Many of these changes have come from the community, which shows one of the benefits of open source development. Some of these advances will probably work their way back into a future version of the regular .NET Framework too.

There have also been improvements to the RyuJIT compiler for .NET Core 2. As just one example, finally blocks are now almost as efficient as not using exception handing at all, in the normal situation where no exceptions are thrown. You now have no excuses not to liberally use try and using blocks, for example by having checked arithmetic to avoid integer overflows.

New in ASP.NET Core 2

ASP.NET Core 2 takes advantage of all the improvements to .NET Core 2, if that is what you choose to run it on. It will also run on .NET Framework 4.7 but it’s best to run it on .NET Core, if you can. With the increase in scope and support of .NET Core 2 this should be less of a problem than it was previously.

It includes a new meta package so you only need to reference one NuGet item to get all the things! However, it is still composed of individual packages if you want to pick and choose. They haven’t reverted back to the bad old days of one huge System.Web assembly. A new package trimming feature will ensure that if you don’t use a package then its binaries won’t be included in your deployment, even if you use the meta package to reference it.

There is also a sensible default for setting up a web host configuration. You don’t need to add logging, Kestrel, and IIS individually anymore. Logging has also got simpler and, as it is built in, you have no excuses not to use it from the start.

A new feature is support for controller-less Razor Pages. These are exactly what they sound like and allow you to write pages with just a Razor template. This is similar to the Web Pages product, not to be confused with Web Forms. There is talk of Web Forms making a comeback, but if so then hopefully the abstraction will be thought out more and it won’t carry so much state around with it.

There is a new authentication model that makes better use of Dependency Injection. ASP.NET Core Identity allows you to use OpenID, OAuth 2 and get access tokens for your APIs.

A nice time saver is you no longer need to emit anti-forgery tokens in forms (to prevent Cross Site Request Forgery) with attributes to validate them on post methods. This is all done automatically for you, which should prevent you forgetting to do this and leaving a security vulnerability.

Performance improvements

There have been additional increases to performance in ASP.NET Core that are not related to the improvements in .NET Core, which also help. Startup time has been reduced by shipping binaries that have already been through the Just In Time compilation process.

Although not a new feature in ASP.NET Core 2, output caching is now available. In 1.0, only response caching was included, which simply set the correct HTTP headers. In 1.1, an in-memory cache was added and today you can use local memory or a distributed cache kept in SQL Server or Redis.

Standards

Standards are important, that’s why we have so many of them. The latest version of the .NET Standard is 2 and .NET Core 2 implements this. A good way to think about .NET Standard is as an interface that a class would implement. The interface defines an abstract API but the concrete implementation of that API is left up to the classes that inherit from it. Another way to think about it is like the HTML5 standard that is supported by different web browsers.

Version 2 of the .NET Standard was defined by looking at the intersection of the .NET Framework and Mono. This standard was then implemented by .NET Core 2, which is why is contains so many more APIs than version 1. Version 4.6.1 of the .NET Framework also implements .NET Standard 2 and there is work to support the latest versions of the .NET Framework, UWP and Xamarin (including Xamarin.Forms).

There is also the new XAML Standard that aims to find the common ground between Xamarin.Forms and UWP. Hopefully it will include Windows Presentation Foundation (WPF) in the future.

If you create libraries and packages that use these standards then they will work on all the platforms that support them. As a developer simply consuming libraries, you don’t need to worry about these standards. It just means that you are more likely to be able to use the packages that you want, on the platforms you are working with.

New C# features

It not just the frameworks and libraries that have been worked on. The underlying language has also had some nice new features added. We will focus on C# here as it is the most popular language for the Common Language Runtime (CLR). Other options include Visual Basic and the functional programming language F#.

C# is a great language to work with, especially when compared to a language like JavaScript. Although JavaScript is great for many reasons (such as its ubiquity and the number of frameworks available), the elegance and design of the language is not one of them.

Many of these new features are just syntactic sugar, which means they don’t add any new functionality. They simply provide a more succinct and easier to read way of writing code that does the same thing.

C# 6

Although the latest version of C# is 7, there are some very handy features in C# 6 that often go underused. Also, some of the new additions in 7 are improvements on features added in 6 and would not make much sense without context. We will quickly cover a few features of C# 6 here, in case you are unaware of how useful they can be.

String interpolation

String interpolation is a more elegant and easier to work with version of the familiar string format method. Instead of supplying the arguments to embed in the string placeholders separately, you can now embed them directly in the string. This is far more readable and less error prone.

Let us demonstrate with an example. Consider the following code that embeds an exception in a string.

catch (Exception e)
{
Console.WriteLine("Oh dear, oh dear! {0}", e);
}

This embeds the first (and in this case only) object in the string at the position marked by the zero. It may seem simple but this quickly gets complex if you have many objects and want to add another at the start. You then have to correctly renumber all the placeholders.

Instead you can now prefix the string with a dollar character and embed the object directly in it. This is shown in the following code that behaves the same as the previous example.

catch (Exception e)
{
Console.WriteLine($"Oh dear, oh dear! {e}");
}

The ToString() method on an exception outputs all the required information including name, message, stack trace and any inner exceptions. There is no need to deconstruct it manually, you may even miss things if you do.

You can also use the same format strings as you are used to. Consider the following code that formats a date in a custom manner.

Console.WriteLine($"Starting at: {DateTimeOffset.UtcNow:yyyy/MM/ddHH:mm:ss}");

When this feature was being built, the syntax was slightly different. So be wary of any old blog posts or documentation that may not be correct.

Null conditional

The null conditional operator is a way of simplifying null checks. You can now inline a check for null rather than using an if statement or ternary operator. This makes it easier to use in more places and will hopefully help you to avoid the dreaded null reference exception.

You can avoid doing a manual null check like in the following code.

int? length = (null == bytes) ? null : (int?)bytes.Length;

This can now be simplified to the following statement by adding a question mark.

int? length = bytes?.Length;

Exception filters

You can filter exceptions more easily with the when keyword. You no longer need to catch every type of exception that you are interested in and then filter manually inside the catch block. This is a feature that was already present in VB and F# so it’s nice that C# has finally caught up.

There are some small benefits to this approach. For example, if your filter is not matched then the exception can still be caught by other catch blocks in the same try statement. You also don’t need to remember to re-throw the exception to avoid it being swallowed. This helps with debugging, as Visual Studio will no longer break, as it would when you throw.

For example, you could check to see if there is a message in the exception and handle it differently, as shown here.

catch (Exception e) when (e?.Message?.Length> 0)

When this feature was in development, a different keyword (if) was used. So be careful of any old information online.

One thing to keep in mind is that relying on a particular exception message is fragile. If your application is localized then the message may be in a different language than what you expect. This holds true outside of exception filtering too.

Asynchronous availability

Another small improvement is that you can use the await keyword inside catch and finally blocks. This was not initially allowed when this incredibly useful feature was added in C# 5. There is not a lot more to say about this. The implementation is complex but you don’t need to worry about this unless you’re interested in the internals. From a developer point of view, it just works, as in this trivial example.

catch (Exception e) when (e?.Message?.Length> 0)
{
    await Task.Delay(200);
}

This feature has been improved in C# 7, so read on. You will see async and await used a lot. Asynchronous programming is a great way of improving performance and not just from within your C# code.

Expression bodies

Expression bodies allow you to assign an expression to a method or getter property using the lambda arrow operator (=>) that you may be familiar with from fluent LINQ syntax. You no longer need to provide a full statement or method signature and body. This feature has also been improved in C# 7 so see the examples in the next section.

For example, a getter property can be implemented like so.

public static string Text => $"Today: {DateTime.Now:o}";

A method can be written in a similar way, such as the following example.

private byte[] GetBytes(string text) => Encoding.UTF8.GetBytes(text);

C# 7

The most recent version of the C# language is 7 and there are yet more improvements to readability and ease of use. We’ll cover a subset of the more interesting changes here.

Literals

There are couple of minor additional capabilities and readability enhancements when specifying literal values in code. You can specify binary literals, which means you don’t have to work out how to represent them using a different base anymore. You can also put underscores anywhere within a literal to make it easier to read the number. The underscores are ignored but allow you to separate digits into convention groupings. This is particularly well suited to the new binary literal as it can be very verbose listing out all those zeros and ones.

Take the following example using the new 0b prefix to specify a binary literal that will be rendered as an integer in a string.

Console.WriteLine($"Binary solo! {0b0000001_00000011_000000111_00001111}");

You can do this with other bases too, such as this integer, which is formatted to use a thousands separator.

Console.WriteLine($"Over {9_000:#,0}!"); // Prints "Over 9,000!"

Tuples

One of the big new features in C# 7 is support for tuples. Tuples are groups of values and you can now return them directly from method calls. You are no longer restricted to returning a single value. Previously you could work around this limitation in a few sub-optimal ways, including creating a custom complex object to return, perhaps with a Plain Old C# Object (POCO) or Data Transfer Object (DTO), which are the same thing. You could have also passed in a reference using the ref or out keywords, which although there are improvements to the syntax are still not great.

There was System.Tuple in C# 6 but this wasn’t ideal. It was a framework feature, rather than a language feature and the items were only numbered and not named. With C# 7 tuples, you can name the objects and they make a great alternative to anonymous types, particularly in LINQ query expression lambda functions. As an example, if you only want to work on a subset of the data available, perhaps when filtering a database table with an O/RM such as Entity Framework, then you could use a tuple for this.

The following example returns a tuple from a method. You may need to add the System.ValueTupleNuGet package for this to work.

private static (int one, string two, DateTime three) GetTuple()
{
    return (one: 1, two: "too", three: DateTime.UtcNow);
}

You can also use tuples in string interpolation and all the values are rendered, as shown here.

Console.WriteLine($"Tuple = {GetTuple()}");

Out variables

If you did want to pass parameters into a method for modification then you have always needed to declare them first. This is no longer necessary and you can simply declare the variables at the point you pass them in. You can also declare a variable to be discarded by using an underscore. This is particularly useful if you don’t want to use the returned value, for example in some of the try parse methods of the native framework data types.

Here we parse a date without declaring the dt variable first.

DateTime.TryParse("2017-08-09", out var dt);

In this example we test for an integer but we don’t care what it is.

var isInt = int.TryParse("w00t", out _);

References

You can now return values by reference from a method as well as consume them. This is a little like working with pointers in C but safer. For example, you can only return references that were passed into the method and you can’t modify references to point to a different location in memory. This is a very specialist feature but in certain niche situations it can dramatically improve performance.

Given the following method.

private static ref string GetFirstRef(ref string[] texts)
{
    if (texts?.Length> 0)
    {
        return ref texts[0];
    }
    throw new ArgumentOutOfRangeException();
}

You could call it like so, and the second console output line would appear differently (one instead of 1).

var strings = new string[] { "1", "2" };
ref var first = ref GetFirstRef(ref strings);
Console.WriteLine($"{strings?[0]}"); // 1
first = "one";
Console.WriteLine($"{strings?[0]}"); // one

Patterns

The other big addition is you can now match patterns in C# 7 using the is keyword. This simplifies testing for null and matching against types, among other things. It also lets you easily use the cast value. This is a simpler alternative to using full polymorphism (where a derived class can be treated as a base class and override methods). However, if you control the code base and can make use of proper polymorphism, then you should still do this and follow good Object-Oriented Programming (OOP) principles.

In the following example, pattern matching is used to parse the type and value of an unknown object.

private static int PatternMatch(object obj)
{
    if (obj is null)
    {
        return 0;
    }
    if (obj is int i)
    {
        return i++;
    }
    if (obj is DateTime d ||
       (obj is string str && DateTime.TryParse(str, out d)))
    {
        return d.DayOfYear;
    }
    return -1;
}

You can also use pattern matching in the cases of a switch statement and you can switch on non-primitive types such as custom objects.

More expression bodies

Expression bodies are expanded from the offering in C# 6 and you can now use them in more places, for example as object constructors and property setters. Here we extend our previous example to include setting the value on the property we were previously just reading.

private static string text;
public static string Text
{
    get => text ?? $"Today: {DateTime.Now:r}";
    set => text = value;
}

More asynchronous improvements

There have been some small improvements to what async methods can return and, although small, they could offer big performance gains in certain situations. You no longer have to return a task, which can be beneficial if the value is already available. This can reduce the overheads of using async methods and creating a task object.

JavaScript

You can’t write a book on web applications without covering JavaScript. It is everywhere.

If you write a web app that does a full page load on every request and it’s not a simple content site then it will feel slow. Users expect responsiveness.

If you are a back-end developer then you may think that you don’t have to worry about this. However, if you are building an API then you may want to make it easy to consume with JavaScript and you will need to make sure that your JSON is correctly and quickly serialized.

Even if you are building a Single Page Application (SPA) in JavaScript (or TypeScript) that runs in the browser, the server can still play a key role. You can use SPA services to run Angular or React on the server and generate the initial output. This can increase performance, as the browser has something to render immediately. For example, there is a project called React.NET that integrates React with ASP.NET, and it supports ASP.NET Core.

If you have been struggling to keep up with the latest developments in the .NET world then JavaScript is on another level. There seems to be something new almost every week and this can lead to framework fatigue and the paradox of choice. There is so much to choose from that you don’t know what to pick.

Summary

In this article, you have seen a brief high-level summary of what has changed in .NET Core 2 and ASP.NET Core 2, compared to the previous versions. You are also now aware of .NET Standard 2 and what it is for.

We have shown examples of some of the new features available in C# 6 and C# 7. These can be very useful in letting you write more with less, and in making your code more readable and easier to maintain.

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here