.NET Standard – Don’t aim for your consumer

Posted by Steven Pears on September 21, 2017 · 3 mins read

When .NET libraries started to aim for the .NET Standard versions rather than particular Framework Versions, it’s fair to say that there was a lot of confusion about what that meant for the people writing the code.

What I’m starting to see now though is that people think they’re on-board, but what’s actually happening is that the developers have just got used to mapping from one to the other.

What does that mean?

I want my target framework to be the LOWEST version that lets my code run as expected, regardless of what’s consuming it

An example:

  • I’m writing a package I want to put on NuGet</p>
  • The package is some basic helper methods that work with Newtonsoft.Json objects.

  • My primary consumer is a .NET Core 1.1 application, which targets .NET Standard 1.6

  • So my package needs to be…?

If you just said .NET Standard 1.6 – you’re absolutely wrong!

Other than my helper logic, the only reference is Newtonsoft.Json and the latest version of that right now still only targets .NET Standard 1.3. There’s no reason to target your package at anything higher than that unless you’re jazzing up your library with some funky new API .NET has only had for a little while.

Why does it matter?

If you’re keeping your code to yourself then it may not make much difference at all. But if you’re writing a NuGet package? Sharing amongst teams in your company or putting it out there for everyone? What happens when your idea grows a little bit and you have a need for the same logic to be used in another application – say…a UWP app for Win10/Xbox?

Well currently (until the next version at least) UWP apps only support .NET Standard 1.4, so now your 1.6 package is unusable by that app. No technical reason that should be the case, other than the fact that you aimed for the .NET Standard version your consumer required, rather than the one you required.

How popular can your NuGet package be if only the very very latest code can run it for no real reason?

OK – I’m aiming at 1.4, but this NetStandard.Library reference is always 1.6.1 – Help!

Yeah – this one threw me for a little bit when I first started getting my head round all this. NetStandard.Library is what’s called a “metapackage”, it contains all the references .NET Standard packages requires, and ensures you’re referencing the appropriate ones depending on your target platform.

The point is that this isn’t affecting the version you’re aiming at, it affects the range of versions you can aim it. Look at Newtonsoft.Json on NuGet – aims for 1.3, still has a dependency on the 1.6.1 MetaPackage. Don’t worry about this one reference, just make sure you’ve got the right TargetFramework set.

So that’s it – aim as low as you can and let everyone enjoy your awesomeness!