Understanding NuGet packages

.NET Core is split into a set of packages. Each of these packages represents a single assembly of the same name. For example, the System.Collections package contains the System.Collections.dll assembly.

The following are the benefits of packages:

  • Packages can ship on their own schedule
  • Packages can be tested independently of other packages
  • Packages can support different OSes and CPUs
  • Packages can have dependencies specific to only one library
  • Apps are smaller because unreferenced packages aren't part of the distribution

The following table lists some of the more important packages:

Package

Important types

System.Runtime

Object, String, Array

System.Collections

List<T>, Dictionary<TKey, TValue>

System.Net.Http

HttpClient, HttpResponseMessage

System.IO.FileSystem

File, Directory

System.Reflection

Assembly, TypeInfo, MethodInfo

Referencing packages

Packages are referenced in the project file; for example, let us explicitly reference the System.Collections.Specialized package version 4.3, so that we can use the NameValueCollection type.

In both Visual Studio 2017 and Visual Studio Code, open Ch16_SharedLibrary.csproj, and add the following markup:

    <ItemGroup> 
      <PackageReference Include="System.Collections.Specialized" 
                        Version="4.3" /> 
    </ItemGroup> 

In Class1.cs, import the System.Collections.Specialized namespace, and declare a public field of type NameValueCollection, as shown in the following code:

    using System.Collections.Specialized; 
 
    namespace Ch16_SharedLibrary 
    { 
      public class Class1 
      { 
        public NameValueCollection stuff; 
      } 
    } 

Understanding metapackages

Metapackages describe a set of packages that are used together. Metapackages are referenced just like any other NuGet package. By referencing a metapackage, you have, in effect, added a reference to each of its dependent packages.

Visual Studio 2017 nicely shows the relationship between metapackages, packages, and assemblies, as shown in the following screenshot:

Understanding metapackages

Note

Metapackages are often just referred to as packages in Microsoft's documentation, as you are about to see.

The following list contains links to some common metapackages and packages, including an official list of their dependencies:

If you were to go to the link for the Microsoft.NETCore.App metapackage, you would see the information shown in the following screenshot:

Understanding metapackages

If you were to scroll down the page, you would see the list of dependencies for the metapackage, as shown in the following screenshot:

Understanding metapackages

Note that Microsoft.NETCore.App 1.1.1 has a dependency on NETStandard.Library 1.6.1.

Understanding Frameworks

There is a two-way relationship between frameworks and packages. Packages define the APIs, Frameworks group packages. A Framework without any packages would not define any APIs.

Note

If you have a strong understanding of interfaces and types that implement them, you might find the following URL useful for grasping how packages and their APIs relate to frameworks such as the various .NET Standard versions: https://gist.github.com/davidfowl/8939f305567e1755412d6dc0b8baf1b7

.NET Core packages each support a set of frameworks. For example, the System.IO.FileSystem package supports the following frameworks:

  • .NETStandard,Version=1.3
  • .NETFramework,Version=4.6
  • 6 Xamarin platforms (for example, xamarinios10)

The two most important package-based frameworks for .NET Core are these:

  • NETStandard.Library: about 40 packages and will support all varieties of .NET with version 2.0 and later
  • Microsoft.NETCore.App: about 60 packages that add console support and other APIs, but limits you to running on .NET Core

Remember---Microsoft.NETCore.App is a superset of NETStandard.Library.

Tip

Good Practice

Choose NETStandard.Library if you are creating a class library that is intended to be referenced by multiple platforms, such as .NET Framework and Xamarin, as well as .NET Core.

Fixing dependencies

To consistently restore packages and write reliable code, it's important that you fix your dependencies. Fixing dependencies means you are using the same family of packages released for a specific version of .NET Core, for example, 1.0.

To fix dependencies, every package should have a single version with no additional qualifiers. Additional qualifiers include release candidates (rc4) and wild cards (*). Wildcards are especially dangerous because it could result in the restore of incompatible packages that break your code.

The following dependencies are NOT fixed and should be avoided:

    <PackageReference Include="System.Net.Http" 
                      Version="4.1.0-*" /> 
    <PackageReference Include="Microsoft.NETCore.App" 
                      Version="1.0.0-rc4-00454-00" /> 

Tip

Good Practice

Microsoft guarantees that if you fixed your dependencies to what ships with a specific version of .NET Core, for example, 1.1, those packages will all work together. Always fix your dependencies.

Switching to a different .NET Standard

It is easy to target a different .NET Standard in a class library.

In either Visual Studio 2017 or Visual Studio Code, modify the Ch16_SharedLibrary.csproj file to use version 1.1, as shown highlighted in the following markup:

    <Project Sdk="Microsoft.NET.Sdk"> 
 
      <PropertyGroup> 
        <TargetFramework>netstandard1.1</TargetFramework> 
      </PropertyGroup> 
 
      <ItemGroup> 
        <PackageReference Include="System.Collections.Specialized" 
                          Version="4.3" /> 
      </ItemGroup> 
 
    </Project> 

In Visual Studio 2017, you will note that when you save the change, you immediately get a restore error, as shown in the following screenshot:

Switching to a different .NET Standard

In Visual Studio Code, in the Terminal, enter the command: dotnet restore, and you will see a similar error message.

The error message tells us that the System.Collections.Specialized package that we want to use is not compatible with .NET Standard 1.1, and that the lowest version of .NET Standard that is compatible is version 1.3.

Modify the version to 1.3 and restore packages. This will fix the error.

Note

Bear in mind that lower version numbers of .NET Standard are always subsets of higher version numbers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.72.176