Teams working on Non-trivial software development projects may need to use components that are either created by them or by some external company. These components can be packaged and distributed using various technologies like NPM, Maven, RPM etc.
In this article, we will see how to create, distribute and consume packages in VSTS, that are created using NuGet.
Concept of NuGet
In 2010, Microsoft introduced the technology of NuGet that makes it easier to package and consume components, resources, contents etc. Before we go to check how VSTS makes package creation and distribution simple, let us understand some of the concepts related to NuGet.
NuGet packages have an extension of .nupkg that are essentially ZIP files containing all the resources to be distributed. It also contains a file with extension .nuspec which is an XML file containing the details of the package including the dependencies.
NuGet packages can be stored at a location that is convenient to the team. These locations are called feeds.
A feed can be an external server hosted by NuGet.org or Microsoft. It can also be internal to the team in a network share or a folder in the file system. NuGet packages are consumed through a command line tool that is integrated in Visual Studio or in the NuGet Package Manager, which is a GUI extension to Visual Studio.
Figure 1 – Package Creation and Consumption Process
Packaging Extension of VSTS
Let us now move on to using VSTS to create a NuGet Package.
Here we are assuming that we are creating a component in the form of a class library using an appropriate project template in Visual Studio. This project itself may consume some external components that are added through some NuGet packages.
We have an option to create a NuSpec file or to retain the details of these used NuGet packages in the project file ( .csproj file). Once we have finished coding the component, we can put that code under version control. We are using VSTS – git as our version control, so we do a commit and then Push changes to a shared repository in a team project on VSTS.
Figure 2 – Code Under Version Control
We will now create a build definition to do a build of this code using the Build service of VSTS.
We will open the builds page from Build and Release menu item. An extension called Packages is required for this build. This extension is available from Microsoft Visual Studio Marketplace.
To install it, let us click the marketplace icon on the top right corner of the page. On the marketplace, search for Packages and install it. Select the subscription name and provide the authorization for this installation.
This extension is a paid extension. It is free for first month, followed by a free use for only 5 users in the subscription. Once it is installed, we will need to create our first feed. This feed will store packages on the VSTS storage itself.
Figure 3 – Create New Feed
Let us give the name of our organization as a name of the feed. I have given the name “SSGS” for my feed. I have made this feed visible to my entire account and allowed it to download packages from any public upstream feed.
Note down the name of the feed and the URL of the feed.
Figure 4 – Open Feed to View URL
The feed name and url will be required later when we consume the packages that we put on this feed.
Build Definition to Create and Push Package
Let us now reopen the Build page. On this page, create a new build definition. Since we are using the class library project, we should select the template of .NET Desktop.
Figure 5 – Create Build Definition With
This template will add all the basic necessary tasks. We can select to use either the Hosted agent or we can install and use the on premises agent.
Figure 6 – Agent and Solution Selection
On the tasks page, we can see various tasks to restore NuGet packages that are used in the project, to compile the project and to copy and publish artifacts. We would like to package the output of compilation into a NuGet package and then push that package on our feed.
Let us now add the NuGet task below the Publish Artifact task.
Figure 7 – Add NuGet Task to Build Definition
This task exposes many operations that are related to NuGet like pack, push, restore etc.
For this newly added task, we will select the Pack operation. For this operation, we will provide the .nuspec file or the .csproj file, whichever may be applicable. This file will provide the upstream packages which are dependencies for our package.
Figure 8 – NuGet Task Pack Operation
After this step, add another NuGet task. This NuGet task will push the created package to our feed. Let us select the Push operation from drop down. Now we will need to select a feed, which is naturally going to be the feed that we had created earlier.
Figure 9 – NuGet Task Push Operation
Now trigger the build.
After the build is successfully over, check the feed to know if the package has been uploaded or not. We will find the package with the same name as our class library that is uploaded by the build.
Consuming NuGet package from out feed
Let us now find out how to consume that package from the feed we just created.
To do so, open Visual Studio. In Visual Studio, open the menu item Tools > Options > NuGet Packages Manager > Package Sources. This usually has a source that is automatically added. The source reflects the feed created by Microsoft for their components that we use in our applications. We will add the source for our feed.
To add the feed as package source, we need the name and the URL of that feed which we noted earlier. Now click on the green + sign at the top (see Figure 10). This opens a dialog box to enter the new source.
Figure 10 – Add NuGet Package Source
We will enter the name and URL of our feed in that dialog box and click the button to connect to that feed. This way, we have added a new source that reflects our NuGet feed in the Visual Studio.
Now we can try to check if this feed provides our uploaded package.
In the Visual Studio, create a new project or open an existing project. In the solution explorer, right click on the project name and select Manage NuGet Packages.
In the window of NuGet Package Manager, select the browse link at the top. Now on the top right corner, from the dropdown for Source Name, select the feed name that we had provided as the package source.
Figure 11 – NuGet Package Selection from Created Feed
It will show the package that was uploaded by the build. Select that package and click the Install button. It will copy the dll that is created by our class library project in our new project and add a reference to it.
Distributing packages that can be used by our team members is made easy by support of Packaging extension in VSTS. It allows to create feed for packages that can be consumed by our team members and build tasks that can create packages and push those to the feed.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on .NET Standard and the upcoming C# 8.0 too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!