In my last post, I showed you how to set up a private NuGet server in TFS2017 and publish a NuGet package to it from a build. But how do you reference and use that package in another solution and/or another build? Well, keep reading and you’ll find out.
I’m going to do this in two phases. First, I’ll show you how to set this up in Visual Studio. Second, I’ll show you how to make this work from a TFS automated build.
Configure Visual Studio 2017 to Use A Custom NuGet Feed
By default, Visual Studio 2017 just knows about the standard, public NuGet feeds. So if you try reference a package that’s in a private NuGet feed like what I showed you in the last blog post, it’s not going to know anything about it.
To begin, you’ll need to know the URL for your private NuGet package feed.
Open a browser and navigate to the Team Project that contains your NuGet package feed. Click on the Build & Release section and choose Packages. Next click on the package feed to view the details. In the upper-right corner, click on the Connect to feed button.
You should now see the Connect to feed dialog. Copy the Package source URL to your clipboard.
Next, open Visual Studio 2017. Go to the Tools menu and choose Options…. In the left panel, locate NuGet Package Manager, expand it, and choose Package Sources. You should now see a dialog that looks like the image below.
Next you need to add a new Package Source. To do this, click on the plus button.
You should now have a new item under Available package sources. Set the name to the name of your package source feed (or some other descriptive value). Under Source, paste in the package source URL that you copied from TFS. Click Update. Click the OK button to close the dialog in Visual Studio.
Add a NuGet Package to a Project from your NuGet Server
Visual Studio now knows about your private NuGet server feed. Next, let’s actually use a package from that feed.
Open up a Solution (*.sln) in Visual Studio. Navigate to the project that you’d like to make a reference from. Right-click References and choose Manage NuGet Packages… from the context menu.
You should now see the NuGet dialog in Visual Studio. In the upper-right of the dialog, change Package source to your private NuGet package feed (example: common-code-feed).
Click on the Browse tab in the upper-left of the dialog and now you should see the packages in your private NuGet feed. Choose a feed and click the Install button in the right-side of the dialog.
If you open the References for your project, you should now see the referenced DLL in the list.
From this point, you can now use this library from your project just like any other piece of code.
Refer to a Private NuGet Server from a TFS Build
If you try to compile this solution using an automated build, you’re going to get an error that says it can’t find the package.
The problem is with the first step — the NuGet restore step. Click on the NuGet restore step to bring up the details.
Next what you need to do is to add an argument that refers to your private NuGet package feed. In the NuGet arguments field, type “-source” and then the URL for your package feed. For example, the value for my demo is “-source http://demo17-tfs:8080/tfs/DefaultCollection/_packaging/common-code-feed/nuget/v3/index.json”.
Save your build definition and re-run your build.
This time your build should succeed.
I hope this helped.
— Need help with automated build, test, and deploy? Need more DevOps awesomeness for your teams and your company? Looking for Scrum training or a therapist for your teams? We can help. Drop us a line at email@example.com.