Step 1: Hosting your own private NuGet feeds
NuGet.Server is a package provided by the .NET Foundation that creates an ASP.NET application that can host a package feed on any server that runs IIS. Simply said, NuGet.Server basically makes a folder on the server available through HTTP(S) (specifically OData). As such it’s best for simple scenarios and is easy to set up.
The process is as follows:
- Create an empty ASP.NET Web application in Visual Studio and add the NuGet.Server package to it.
- Configure the Packages folder in the application and add packages.
- Deploy the application to a suitable server.
Create and deploy an ASP.NET Web application with NuGet.Server
- In Visual Studio, select File > New > Project, set the target framework for .NET Framework 4.6 (see below), search for “ASP.NET”, and select the ASP.NET Web Application (.NET Framework) template for C#.
- Enter the application name, click OK, and in the next dialog, select ASP.NET 4.6 – Empty template and click OK.
- Right-click on the project, select Manage NuGet Packages, and in the Package Manager, search and install the latest version of the NuGet.Server package if you’re targeting .NET Framework 4.6.
- Installing NuGet.Server converts the empty Web application into a package source. It creates a Packages folder in the application and overwrites web.config to include additional settings.
- To make packages available in the feed when you publish the application to a server, add their .nupkg files to the Packages folder in Visual Studio, then set their Build Action to Content and Copy to Output Directory to Copy always:
- Run the site locally in Visual Studio.
- Click on here in the area outlined above to see the OData feed of packages.
- By running the application the first time, the Packages folder gets restructured into a folder for each package. This matches the local storage layout introduced with NuGet 3.3 to improve performance. When adding more packages, continue to follow this structure.
- Once you’ve tested your local deployment, you can deploy the application to any other internal or external site as needed.
- Once deployed to http://<domain>, the URL that you use for the package source will be http://<domain>/nuget.
Step 2: Publish your NuGet Server
- For demo purpose, I am deploying in to my local IIS server. Open IIS and create an empty site in it.
- Under Bindings, select IP address of your PC, then click on Ok.
- Now, Right click on your project and select Publish option. On publish window select IIS, FTP option as shown below (VS2017).
- Enter Server name (nothing but your PC name), Enter Site name that you created in IIS, click on validate, Next and save. It starts publishing now.
- Once it’s published successfully; you can browse the site with your IP address.
- In browser, you will see something similar as shown below.
Step 3: Enable Basic Authentication to your feed
- To enable your feed private with authentication, you can do that by enabling Basic Authentication in IIS.
- Go to IIS, select Authentication under IIS category.
- Enable Basic Authentication.
- Now, add a local user. Go to start menu and search for Computer Management, open it.
- Expand Local Users and Groups, right click on Users and select New User.
- Enter user name and password, select password never expire and then click on create.
- Now navigate to InetPub->wwwroot and right click on DemoNuGetFeed folder where we published your NuGet server and select properties.
- Under properties, go to Security, select DemoFeedUser from user list and then click on Edit. Give read/write permissions and then click on Apply and Ok.
- Now try to browse your server, it should prompt you for user name and password as shown below.
- This step is only for users connected to a router, if you are connected to a router then you need to enable port forwarding to your machine so that all the requests coming to port 80 will automatically redirect to your PC.
To do this, login to your router and go to port forwarding settings and add Port no 80 and select your machine IP address. The settings will look like below.
Service Port and Internal Port are 80 because in my machine IIS I configured my NuGet server port number with 80. IP address is the machine where your NuGet server running/published.
- Now if you browse the url with your IP address it should automatically navigate to your NuGet server as shown below. Now you can access your NuGet feed from anywhere in the world using http://<IPAddress>/nuget.
Step 4: Configure your private feed in VSTS build
- Open your Solution/Project where you want to use your private feed in Visual Studio.
- Add nuget.config file in to the project. It should have the following structure.
- Add your feed URL under PackageSources tag as shown below.
- Check-In and Push the code to VSTS.
- Login to VSTS, Navigate to your team project.
- Go to Builds, and create a new build definition with build template which suits to your project.
- Now add a task, select NuGet task and click on add.
- Select nuget restore step which you added in above step, in right side configuration area expand Feeds & Authentication section. In that enter/select path to nuget.config file as shown in below screenshot.
- Now add credentials for each feed by clicking on plus icon.
- On Add new Nuget connection window, select Authentication type here for example we selected Basic Authentication.
- Save the Build definition and queue a new build.