Monday, March 12, 2018

Package Management in .NET Core

When developing .NET applications, developers use Nuget, Visual Studio and PowerShell. But what about .NET Core?

Introduction

In development, it's common to import and reuse routines from external libraries. That code is often code is bundled into packages/libraries that could contain compiled code (as DLLs) along with other content needed in the projects that consume these packages (CSS, images, JavaScript, etc).

If you use .NET you have already dealt with Nuget one way or another. But how does that work in .NET Core?

Nuget and .Net Core

Nuget remains strong in .Net Core as it's the default tool to download packages. For more info regarding Nuget, please visit: https://docs.microsoft.com/en-us/nuget/what-is-nuget

If you have Visual Studio installed in your machine, probably already have nuget.exe installed because it already contains a built in Nuget package manager which does not mean that nuget.exe is installed on your machine.

With .Net Core, package maintance is done using the new dotnet cmdlet.

Nuget

For .NET, the Microsoft-supported mechanism for sharing code is NuGet, which defines how packages for .NET are created, hosted, and consumed, and provides the tools for each of those roles.

Nuget.org is where most of these packages (free or not) are hosted and can be downloaded. Also note that you also can host your own nuget server containing your team's internal packages.

Managing Nuget Packages

In order to build, push, modify and manage nuget packages, we need to use the nuget.exe tool. If you're interested in managing your own packages, please take a look at this documentation.

Installing nuget.exe

As nuget.exe itself is not included with any version of Visual Studio, if you want to use it, you can then go and download it on nuget.org/downloads. Please download the latest.

Also, don't forget to set it on your %path% so it's accessible from the command prompt everywhere.

Nuget Package Exlorer

I also recommend installing Nuget Package Explorer for a friendly nuget.exe user interface.

The Global Packages Folder

After Nuget3, there is now a packages folder that is shared for all projects that you work with. Packages are downloaded and stored in the %userprofile%\.nuget\packages folder so you will only have one copy of that package on your machine. The same package can be reused for new projects saving time and band, especially on build servers.

The following command line lists you all packages installed in your system, for your user:
nuget locals global-packages -list

Adding nuget packages to your .Net Core project

Adding packages a your solution can be done two ways: by using Visual studio or from the command line.

Adding using the command line:

Using .Net Core, in the project folder, run the following command:
dotnet add package <package-name>

Adding packages using Visual Studio:

Adding with VS is simply right-clicking the web project name -> Manage NuGet packages:

Restoring packages

The dotnet restore command restores the dependencies and tools of a project But you don't need to run it after adding a new package because it's implicitly run by all commands, such as dotnet build and dotnet run.

In your solution folder, run:
dotnet restore
For more info, visit: https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-restore?tabs=netcore2x

Conclusion

With access to command line tools it is way easier to write scripts, automate, manipulate multiple packages at once just by using the command line. Being a very important to know due to the, I usually urge .Net developers to read more about it. For more information on command line reference, visit: http://docs.nuget.org/docs/reference/command-line-reference

Still on Nuget packages, also spend some time understanding how they are built and shared. May be a good fit for your project.

See Also

.NET Core and .NET merging as .NET 5.0
Creating a Ubuntu Desktop instance on Azure
Why you should start using .NET Core
Building and Running ASP.NET Core apps on Linux
Generating ASP.NET / ASP.NET Core views in the backend

For more posts on .NET Core, please click here.

References

NuGet 3 - What and Why?
NuGet CLI reference
.NET Core Command-Line Interface (CLI) Tools
 Photo by Christian Wiediger on Unsplash