Creating your own NuGet Server
Having our own NuGet Server can help us to generate a package with each version of the library and have it available for each project and member of the development team. By following the steps below you can create our own NuGet Server, make a package, publish it and install it in any project.
Creating NuGet Server
First we need to create an Empty Web Application and add the NuGet.Server package.
1.Creating an empty web application for a NuGet server.
2.Adding the NuGet.Server package to the project.
3. Running the project we will have the NuGet Server working.
Publishing the NuGet Server in IIS
1.Creating the website on IIS that hosts the Nuget Server
The first thing you must do is create a Website, for that, after opening the IIS administration window, we right-click on Sites and then Add Website… which will open a window to indicate the details of the new Website, as the name, the path where the files will be and the port. This is the basic configuration.
2. Publishing our NuGet Server in IIS
Now we are going to publish the project where we have our NuGet Server in the website that we have just created: basically we right click on the name of the project and then on Publish… this will open a window where we must select the target, and indicate the destination site data, as seen in step 2 of the image, save the information and then click on Pusblish.
3. Configuring the apiKey in NuGetServer
It is also necessary to configure an apiKkey in the Application Settings (web.config), which should be indicated when publishing a package. In this case I put the value 809.
With this, our server is ready to receive and serve NuGet packages.
Creating and publishing a package
We are almost ready to create our first package, which we can version and have it available from our NuGet Server.
1. Install NuGet.CommandLine
First we need to install the NuGet.CommandLine package in the project we want to create a package, in my case create a test project called MyLibrary. The NuGet.CommandLine will allow us to execute NuGet commands in our project.
2. Creating a package using NuGet Pack
NuGet Pack is the command that creates the package that we want to publish to the server, that is, a file with extension .nupkg, of the project that we indicate. This command can be executed in the Package Manager Console and takes as an argument the path of the project and the configuration in which we want it to be created, in this case: release, that is, ready to be used in production.
The description, the author, the company, version and other information of the package must be indicated in the properties of the project. Once this is done we can execute the command:
NuGet Pack .\MyLibrary\MyLibrary.csproj -Properties Configuration=release
after run the command, the package MyLibrary.1.0.0.nupkg will be created in the project’s solution folder.
3. Publish the package using NuGet Push
NuGet Push is the command that will allow us to publish our package to NuGet Server, we have to indicate the package path, the server url and the apiKey that we configured previously.
NuGet Push .\MyLibrary.1.0.0.nupkg -Source http://localhost:2019/nuget -ApiKey 809
Installing the NuGet Package
Since the package is our Nuget Server we can install it, but first we must add it as a source of packages in the NuGet Package Manager, for that we right-click on a project, then click Manage NuGet Package… and add the url of our NuGet Server as a source of packages.
With this we can install our package in any project.