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?
Photo by Christian Wiediger on Unsplash
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 in the .NET world. Nuget.org is where most of these packages (free or not) are hosted and can be downloaded. If you have Visual Studio installed in your machine, probably already have nuget.exe installed since  VS only wraps NuGet when managing the packages of your projects. With .NET Core however, package maintenance is done using the new .NET Core CLI.

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.
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. It will help you understand your package, its dependencies and metadata. NuGet Package Explorer looks like this:

The Global Packages Folder

After NuGet 3, 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 packages using the command line:

I'm a strong supporter of using the CLI. So let's start with it first. In your project folder, run the following command:
dotnet add package <package-name>
As an example, this is how I added jQuery to an old JS project:

Adding packages using Visual Studio:

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

Restoring packages

To finish, you can also to restore packages using the command line. In your solution folder, run:
dotnet restore

Conclusion

On this post we reviewed how to manage NuGet packages using both Visual Studio and the new .NET Core CLI. Since most developers are used to Visual Studio and the experience won't change much between .NET Framework and .NET Core projects, on this post I empathized how to use the CLI. Using the command line it a great way to learn your tools, write scripts, automate and manage your packages more quickly.

References

See Also

    About the Author

    Bruno Hildenbrand      
    Principal Architect, HildenCo Solutions.