Setting Up NuGet.Server To Host Your Own NuGet Feed (PowerShell Repository)
Think you can do better? Show us!
NuGet.Server is a package provided by the .NET Foundation that can be used to create an ASP.NET application for hosting a NuGet package feed in IIS. In this video, Matt will walk you through the steps of creating an empty web application in Visual Studio and install the latest version of the NuGet.Server package which converts the empty web application to an ASP.NET application that can host a pakage feed on any server running IIS. Matt will also go over how to modify the web.config file to change the default package location and to specify and API key for publishing/deleting packages. Once the application is ready, Matt will show you how to deploy it to a server running IIS. Finally, you will see how easy it is to register the new NuGet.Server feed in PowerShell using Register-PSRepository.
In this snip, I'm going to go through setting up NuGet server to host your own nuget feed to use as a powerShell repository nuget server is a package that can be used to create an ASP.net application for hosting a nuget package feed in IIS to begin you're going to need to have visual studio installed and a server to run IIS let's go ahead and configure. The Web Server first here. I have some powershell code that I'm going to use to configure IIS on my web server, my web server is called web 01. So I'm going to store that in my web server, variable next. I have a script block containing the code that I'm going to use to configure the server. In my windows features variable I'm storing a list of web. Windows features that will be required to run the nuget server. Then I'm going to pass that variable to install windows feature to install IIS and all those necessary components. Finally, I'm going to use new item to create a folder under C Inetpub. WWW root called feeds then I'm going to use new SMB share to create a share called feeds at that location, and I'm going to give my domain account change. Access so I can deploy my application files to the file share. Let's go ahead and run this code with invoke command to configure. The server now that IIS, is installed on the Web server. Let's switch over to Visual Studio in order to deploy nuget server to IIS. We need to create an ASP.net web application to do that. Let's go to file new and then project in the search box. Search for ASP.net web application. Select the ASP.net web application for Visual C sharp give the project, a name. I'm going to call it. Nuget server also make sure the framework is set to .net framework 4.6 or higher, then click OK. On the new ASP.net web application screen select empty and then click OK. Once the empty ASP.net web application is created right click on the project under the solution Explorer and click. Manage nuget packages in the nuget package manager window click on browse. Then search for nuget dot server. Select nuget dot server from the search results and install the latest stable version. When you install the nuget server package. It will make some changes to your solution and convert your empty web application into a package source. Click OK if you are asked to proceed then click to accept the license acceptance. Installing the nuget server package will set up a packages folder as well as modify the web dot config file with the settings that are required. Let's go ahead and close the nuget package manager and open the web dot config file. Microsoft warns that you should carefully inspect the web dot config file after the modifications have been completed. Nuget server may create duplicate elements in this file instead of overwriting them in particular. Look for any duplicate compilation elements. If there are more than one simply just delete the oldest version and save the file. Next if you want to have any packages available on the nuget server when it's deployed you can place the NUPKG files in the packages folder of the project. Right click on the packages folder, then go to add then click on existing item browse to the location of the package file select it and then click. Add I'm going to add a test module package for this demonstration if you're adding any package files make sure to right click on the file and click properties, then make sure that build action is set to content and copy to output directory is set to copy always. Now let's take a look at a couple of elements in the web dot config. If you would like to allow users to push or delete packages from the server you will need to specify an API key on line 51. There is a key called require API key when set to true an API key will be required to push or delete packages from the server if require API key is set to true. On Line 57, you will need to set the value of API key to whatever you choose to use as an API key. I'm going to just put. 123456. Next. Let's take a look at line number 63. Here, we have a package is path key if this value is left blank. The packages path will default to the packages folder inside the project. You can also specify an alternate path where you would like the package files to be stored on the server. I'm going to leave this blank to use the default click save to save all the changes. Now we can test the web application by running it locally through Visual Studio first. I'm going to click on the arrow. Where it says IIS express' then I'm going to go to web browser here. I want to make sure that Internet Explorer is selected. I had an issue viewing packages with the edge browser, but Internet Explorer seems to work fine. Now let's click debug then start without debugging. When the nuget server homepage. Loads you should be able to see any errors if there are any. If there are no errors the package feed URL will be displayed on this page as well as the location of the package files if you click on the here link in the click here to view your packages you should be able to see any packages that you added to the packages folder. This is the link that did not work. Under the edge browser for me. Now let's closeout of Internet Explorer at this point of the process. I notice an issue. If we right click on packages and open the folder in Explorer. You can see that the nuget server took the package file I placed in here. And created a folder structure to store it and it's information. The problem with this is that when we go to publish this application. It will fail at the build because it is going to be expecting. That package file to be there. Another issue I have which seems to be a known issue. Currently is the debugger doesn't stop after I close the browser. Even though I have set it to do so. So I'm going to go ahead and close visual studio and reopen. It otherwise when I drop another package file into the packages directory. It's going to break it out into the same folder structure again. So just keep that in mind. Let's go ahead and Recopy. The test module to the packages folder also let's delete the test module folder that the nuget server created. Now we're ready to deploy the application to the Web Server. Let's go to build then publish nuget server. For publish target I'm going to select folder, then I'm going to enter the path to the feeds share that I created at the beginning of the video. Next click on publish this will run a build and copy the files for the web application over to the Web server. Once the application has been published. Let's switch over to the Web Server under the WWW root folder. You can see the feeds folder that we created and under that folder. The files for the application next. We need to open the IIS manager. In IIS manager expand the server name, then sites then default website. You can see the feeds folder under here for the nuget server to work. We need to right click. The feeds folder then click. Convert to application choose. The application pool that you would like to use. I'm going to keep the default app pool and click OK next. We need to go to application pools to modify the identity of the app pool right click on the default app pool and go to advanced settings. Right now, it is set to use the application pool identity. Let's go ahead and modify this and change it to use the local system. Instead, this will ensure that the application will have access to the packages folder now. The Web server. Configuration is complete. Let's go back to the client machine where visual studio is installed and open Internet Explorer. Now let's browse to the new application on the Web Server. The nuget server is now up and running and we can click to view the packages to see the test module. Now that the feed is set up let's test it out by registering it with powershell. If I run get PS repository. You can see that I currently only have the PS gallery registered. Let's go ahead and Register. The new nuget feed. I'm going to create a hash table. So I can splat the following parameters to register PS repository. For name I'm going to call it nuget feed for both source location and publish location. I'm going to use the URL that I got from the nuget server homepage. And finally I'm going to set the installation policy to trusted. Now let's go ahead and register the new feed. When I run get PS repository again, I can see the new feed has been registered finally let's go ahead and test the new feed out by running. Find module against it and there is the test module that we deployed with the application and that was setting up nuget server to host your own nuget feed as a powershell repository.