Category Archives: DevOps

Connecting On-Premise/private NuGet packages or feed URL in source code from VSTS for build and deploy

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:

  1. Create an empty ASP.NET Web application in Visual Studio and add the NuGet.Server package to it.
  2. Configure the Packages folder in the application and add packages.
  3. Deploy the application to a suitable server.

Create and deploy an ASP.NET Web application with NuGet.Server

  1. 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#.
    NuGetServer_001
  2. Enter the application name, click OK, and in the next dialog, select ASP.NET 4.6 – Empty template and click OK.
    NuGetServer_002
  3. 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.
    NuGetServer_003
  4. 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.
    NuGetServer_004
  5. 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:
    NuGetServer_005
  6. Run the site locally in Visual Studio.
    NuGetServer_006
  7. Click on here in the area outlined above to see the OData feed of packages.
    NuGetServer_007
  8. 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.
    NuGetServer_008
  9. Once you’ve tested your local deployment, you can deploy the application to any other internal or external site as needed.
  10. Once deployed to http://<domain&gt;, the URL that you use for the package source will be http://<domain>/nuget.

Step 2: Publish your NuGet Server

  1. For demo purpose, I am deploying in to my local IIS server. Open IIS and create an empty site in it.
    NuGetServer_009
  2. Under Bindings, select IP address of your PC, then click on Ok.
    NuGetServer_010
  3. Now, Right click on your project and select Publish option. On publish window select IIS, FTP option as shown below (VS2017).
    NuGetServer_011
  4. 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.
    NuGetServer_012
  5. Once it’s published successfully; you can browse the site with your IP address.
    NuGetServer_013NuGetServer_014
  6. In browser, you will see something similar as shown below.
    NuGetServer_015

Step 3: Enable Basic Authentication to your feed

  1. To enable your feed private with authentication, you can do that by enabling Basic Authentication in IIS.
  2. Go to IIS, select Authentication under IIS category.
    NuGetServer_016
  3. Enable Basic Authentication.
    NuGetServer_017
  4. Now, add a local user. Go to start menu and search for Computer Management, open it.
    NuGetServer_018
  5. Expand Local Users and Groups, right click on Users and select New User.
    NuGetServer_019
  6. Enter user name and password, select password never expire and then click on create.
    NuGetServer_020
  7. Now navigate to InetPub->wwwroot and right click on DemoNuGetFeed folder where we published your NuGet server and select properties.
    NuGetServer_021
  8. 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.
    NuGetServer_022
  9. Now try to browse your server, it should prompt you for user name and password as shown below.
    NuGetServer_023
  10. 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.
    NuGetServer_024
    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.
  11. 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

  1. Open your Solution/Project where you want to use your private feed in Visual Studio.
  2. Add nuget.config file in to the project. It should have the following structure.
    NuGetServer_025
  3. Add your feed URL under PackageSources tag as shown below.
    NuGetServer_026
  4. Check-In and Push the code to VSTS.
  5. Login to VSTS, Navigate to your team project.
  6. Go to Builds, and create a new build definition with build template which suits to your project.
    NuGetServer_027
  7. Now add a task, select NuGet task and click on add.
    NuGetServer_028
  8. 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.
    NuGetServer_029
  9. Now add credentials for each feed by clicking on plus icon.
    NuGetServer_030
  10. On Add new Nuget connection window, select Authentication type here for example we selected Basic Authentication.
    NuGetServer_031
  11. Save the Build definition and queue a new build.