.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:
The following table lists some of the more important packages:
Package |
Important types |
|
|
|
|
|
|
|
|
|
|
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; } }
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:
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:
If you were to scroll down the page, you would see the list of dependencies for the metapackage, as shown in the following screenshot:
Note that Microsoft.NETCore.App
1.1.1 has a dependency on NETStandard.Library
1.6.1.
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.
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:
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 laterMicrosoft.NETCore.App
: about 60 packages that add console support and other APIs, but limits you to running on .NET CoreRemember---Microsoft.NETCore.App
is a superset of NETStandard.Library
.
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" />
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:
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.
3.138.37.20