PM's Blog

Pramod Mohanan's notes about ASP.NET, MVC, C#, SQL, jQuery, Bootstrap

Creating a Nuget Package using Visual Studio

Upon experimenting with various methods of creating NuGet packages manually and otherwise, I find this process quite straight forward and easy. This involves using a template created by Danny Varod. The following are the step-by-step process to be followed

Get the template if you haven’t already
Install the visual studio extension that adds the NuGet package template to your Visual Studio

Create Project Using the NuGetPackage.Project.NuGet template

In the project, you will find the following files

  • internalpackages.config
  • NuGetPack.config
  • packages.config
  • For Starters, the below extract from the readme.txt file is quite explanatory
    1. You can modify the package metadata by editing NuGetPack.config.
    2. To include projects in this package, add project references to this project.
    3. To include dependencies on external NuGet packages, add NuGet package references to this project.
    4. To include dependencies on other NuGet packages created in this solution, add them to the internalPackages.config file.
    5. To synchronize the package version with the version of the assemblies included in it, use a shared AssemblyInfo.cs file for the source projects and the NuGet package projects.
    6. To add system reference to the package, add them as references to this project and set AddFrameworkReferences to true in the config file.

    Add the required assembly, its dependencies to the project
    We can now add the library as reference like we normally do, either from a project, a standalone assembly file(dll etc.) or via NuGet Package Manager. Once done it would look something like this

    Edit the Metadata for the package
    Edit the file NuGetPack.config and update description, summary, logo, website etc. This is what will be shown on the NuGet Package manager modal/console.

    <NuGetPack>
      <AddFrameworkReferences>false</AddFrameworkReferences>
      <IconUrl>http://pramodmohanan.net/my-favicon.ico</IconUrl>
      <ProjectUrl>http://pramodmohanan.net/</ProjectUrl>
      <LicenseUrl>http://pramodmohanan.net/my-license-page</LicenseUrl>
      <Authors>Pramod Mohanan</Authors>
      <Owners>Pramod Mohanan</Owners>
      <Copyright>My Copyright Line</Copyright>
      <Description>This is sample library created for my blog post</Description>
      <Summary>Adds My Library to your project. This library was created as a sample for my blog post.  However it has a class with helloworld function :)</Summary>
      <Keywords>MyLibrary, HelloWorld</Keywords>
    </NuGetPack>

    Install the PubComp.NuGetPack in your project
    In Nuget Package Manager, look for PubComp.NuGetPack by Danny Varod and install it. This should add a folder with NuGet.exe and NuGetPack.exe which is used for creating the package post the compilation. (Check the post-build event command line project properties – it uses these files). Once installed it looks like this in the solution explorer

    Build the Project using Release (Any CPU) configuration
    After building the package you should see the .nupkg file in your release folder. This is the nuget package ready to go into any Nuget repository.

Leave a Reply

Your email address will not be published. Required fields are marked *

PM's Blog © 2018