The major purpose from this post is to give simple and clear hands on how to create your first Nuget repository and publish Nugets to it to use in other projects, and indeed hands on how to build your project as a Nuget package.
As one of the products i am working on now, the arch. is a MicroService oriented which is perfect for the Product, but when we think more behind the scene , how can we organize the whole solution artifacts and components separately with respect to the fact to still have some common services to handle such a generic or shared logic.
Here i found to build our own solution NuGet repository is a suitable idea to handle the whole things instead of us.
How To start
- Define the common services, (e.g. ImageProcessing Service), Encryption Service, System Models or MicroService Models (usually these models is shared between the MicroServices).
- Define the Naming convention you will use later for (Nuget Name, Description, Author).
How to create a Nget packege for the progect ?
- Assuming you have a separate project performing a functionality to use later in various projects (e.g. ImageCompression Service).
- There are many ways to package your project as a Nuget, but in this case i will use an easy and stable way to “Create a Nuget Package With Every Successful Build”.
- In project (e.g. ImageCompression), install Nuget package from nuget.org with Id (CreateNewNuGetPackageFromProjectAfterEachBuild), you can do it by the Visual Studio Nuget Manager or directly from the Tools > Nuget Package Manager > Package Manager Console.
- Installing this Nuget will add a (_CreateNewNuGetPackage) folder in the project, (Config.ps1) is the file where you can customise to change the way the Nuget qill be created.
- If you build your project now, a .nupkg file will be created with AssemplyName and extra info about the build (e.g. Debug or Release, X86 or AllCPU or whatever …), read the coming points to figure out how to change the Nuget Info. and Name.
- And to control the Nuget Information (Name, Description, Author, Version), follow these points.
- Open the project (AssemblyInfo.cs) file inside (Properties), Setting the [AssemblyDescription] will set the Nuger Description, Setting the [AssemblyCompany] will set the Nuget Author.
- Open (Config.ps1), find [$versionNumber = “”] at line 19, set the version number or keep it empty and it will use the default build version, find [$appendConfigurationAndPlatformToNuGetPackageFileName = $true] at line 50, keep it true and the Nuget name will contains the Platform and Configuration info (e.g. Comp.ImageCompression.126.96.36.199.Debug.AnyCPU.nupkg) or set to false and it will be shorter like (e.g. Comp.ImageCompression.188.8.131.52.nupkg)
- Now you have the Nuget (.nupkg) and you are ready to publish.
Publish your Nuget
We are here talking about your private repository starting from (Local machine repository to cloud hosted repository).
Local Machine Repository
- Create a Directory at your local machine (e.g. c:\\MyNugetRepo) and push your .nupkg file(s) to it.
- from Visual Studio, open Tools > Nuget Package Manager > Package Manager Settings
- open from left menu, Nuget Package Manager > Package Source
- Click on the green (+) Btn. to add new source
- Write the source Name and Source to (c:\\MyNugetRepo)
- Click ok and now you have a new custom private local machine Nuget Source.
Site (Shared Repository)
I mean a shared Site (URL), either network on company IIS server or to cloud site to access from outside the network,. its all depends on your need.
- Create a new project of type (ASP.net Empty Web Application)
- Install the Nuget Package (Nuget.Server) by .net foundation on this Application, this will re structure the whole app to be a Nuget Feed App.
- a (Packages) folder now created and ready to contain all your .nupkg(s)
- Publish the App to (IIS) or (Cloud based site) (e.g. AZURE app service)
- When publish done, open the site and the default page should be ibstructions for the Nuget(s) repository URL and also how to push new .nupkg, for me, you can push new Nuget package to this Repository by simply add the .nupkg to the (Packages) folder and re publish again.
- be noted that you will need to use the URL (e.g. http:// mysiterepo:2255/nuget) as the Repository Source.
- also note that ot might take few minutes until the package displayed in the list of available packages after installation.
More Info …
The (CreateNewNuGetPackageFromProjectAfterEachBuild) handles the dependencies from the installed packages, so ant project installed packages will be added to the project package dependencies.