Recently get back into .net world again, was doing some stuff and wish to have something like Maven Repository that manages the libraries. So NuGet is the .net version of Maven. I prefer to host it locally.
References:
Microsoft - Hosting your own NuGet feeds
Microsoft - Setup Nuget-server
There are quite a lot of information out there, but I want to write one myself just a reminder of what I did to make it work:
My Machine:
- Visual Studio 2017
- Windows 10
- IIS (Internet Information Service (Version 10)
- NuGet.Server - Version 2.14.1
Create APS.NET Web Application with NuGet.Server
- In Visual Studio 2017 > New > Project > Select "ASP.NET Web Application (.NET Framework".
- Right click on the Project's References > Select "Manage NuGet Packages...".
- Find "NuGet.Server" and install the latest version. It will change your empty webapp into NuGet.Server app.
- Accept the override of web.config files and accept any changes it prompted.
- At this time, your Project Structure looks like this
- Update the "Web.config".
- apiKey: it is used for nuget push command.
- Example: "<add key="apiKey" value="Test123ApiKey"/>"
- packagesPath: leave black if you want to use the default "Packages" folder in the project.
- Example: "<add key="packagesPath" value="C:\Vincent\NugetPrivateInstancePackages"/>"
- added "identity" on system.web block - this user should have the administrator account that will not have problem creating cache / temp folder. Issue seen when you load "http://localhost/nuget/Packages" url from the homescreen.
- Example: <system.web><other stuff.....><identity impersonate = "true" userName = "MyAdminAcc" password = "password_for_user">/identity></system.web>
Deploy on IIS
- I create my own website from IIS Manager.
- Give a Site Name.
- Give a Physical Path.
- Give a Bindings port other than 80.
- Copy the files over to the directory. Here's example of the files needed.
- Setup "Application App" for your website.
- Right click on newly created WebSite from the IIS Manager > Select "Add Application".
- Configure:
- Give an Alias.
- Select the Physical Path.
- Pick ".Net 4.5" as your Application pool.
- At this point you will be able to load the webapp by going to the browser: http://localhost:8081/MyNuget, you will see something like this.
Create Nuget Packages
Once you have your NuGet.Server private instance ready, it is time for you to put your NuGet packages up there. Here's how you create your NuGet Package.
- Visual Studio 2017 > New > Project > Select "Class Library (.NET Framework)".
- Add a "HelloWorld" Class.
public class HelloWorld
{
String _name;
public HelloWorld()
{
}
public HelloWorld(string name)
{
Name = name;
}
public string Name { get => _name; set => _name = value; }
public string GetGreeting()
{
return String.Format("Hello {0}", Name);
}
}
- Click Build successfully.
- Install Nuget.CommandLine to allow running NuGet Command line from Visual Studio 2017.
- From Visual Studio 2017 > Tools > NuGet Package Manager > Select "Package Manager Console".
- Execute Command: Install-Package NuGet.CommandLine
- Run Command to create "NuGet Spec" from where the project file ".csproj" is located.
- Still in the "Package Manager Console". Make sure you are in the right directory.
- Execute Command: nuget spec
- You will see that a new file "MyLibrary.nuspec" is created in the same directory.
- Edit the "MyLibarary.nuspec".
- Before:
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
<projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
<iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>Tag1 Tag2</tags>
</metadata>
</package>
- After:
<?xml version="1.0"?>
<package >
<metadata>
<id>Vincent.MyLibrary.GreatUtilities</id>
<version>$version$</version>
<title>$title$</title>
<authors>Vincent</authors>
<owners>Vincent</owners>
<licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
<projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
<iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>For Internal Testing Only.</description>
<releaseNotes>This is the First Release</releaseNotes>
<copyright>Copyright 2017</copyright>
<tags>Utility MyLibrary Vincent</tags>
</metadata>
</package>
- Package your NuGet Library.
- Still in the "Package Manager Console". Make sure you already build the project.
- Execute Command: nuget pack
- For Configuration Specific: nuget pack -Prop Configuration=Release
- It will create a "nupkg" file in the same directory. It looks like this with both "nuspec" and "nupkg".
- This is all you need to have "nupkg".
Push NuGet Package to your NuGet.Server private instance.
There are 2 ways to add NuGet Packages file to "NuGet.Server".
- Manually copy the "NuGet Package" to the "Packages" directory.
- Push the "NuGet Package" using the "nuget.exe" push command.
- Copy "Vincent.MyLibrary.GreatUtilities.1.0.0.nupkg" to Website "Packages".
- From the URL, Click "here" and it will extract and generate the packages ready for consumed.
- Your "Packages" folder will look like this.
Method 2: Push the "NuGet Package" using the "nuget.exe" push command.
- From Visual Studio 2017, "Package Manager Console". Make sure you are in the right directory.
- Execute command: nuget push .\Vincent.MyLibrary.GreatUtilities.1.0.0.nupkg VincentTest123 -Source http://localhost:8081/MyNuget/api/v2/package
- It will create the same folder structure in "Packages" folder on the NuGet.Server
How to Consume the Hosted NuGet Library?
The Default NuGet Package Manager is set to "nuget.org" on "https://api.nuget.org/v3/index.json". The idea is to add another package sources to use the Private NuGet.Server that we have setup.
- In you New Project. Bring up "NuGet Package Manager". Click the "Setting" icon for "Package source".
- Click the "Add" button to create another Package Source.
- Give Name.
- Give Source to your private URL.
- Once that is done, select the "Package Source" that you have setup. and it will automatically show your Library.
- The rest is just matter of adding and using it.
Hope it is useful for you.
No comments:
Post a Comment