Creating a Dotnet Core NuGet Package

Get ready to change the way people code with your own awesome new open source NuGet package.

-

My name is Rich Minchuk. I’m developing an open source project called GHK. It’s based around a few simple concepts:

  1. Develop a tool that bloggers can use, right out of the box, to grow their personal brand.
  2. Use brand new tech to show off what I’ve learned while building it.
  3. Make it unbelievably simple to install.
  4. Rely on mostly free tools to bring exceptional value to users.

I’m opting for a streamlined approach to this blog and future posts, so let’s dive right in.

Repo

Set up a GitHub account. If you don’t know Git, I’m going to point you to my read me markdown file which contains a number of commands that may help get you started.

Once you have a GitHub account, you will need to create a repository in which your open source project can live. Once your repo is set up and you can push commits to it, we can move on.

Next, let's get set up in dotnet core if you aren’t already. I’m on a Mac so the commands and steps might be a little different depending on your OS flavor. These steps were written and tested using .net core version 1.4 (IE: after they went back to .csproj files):

  1. Install Visual Studio Code and its C# extension.

  2. (MacOS only) Install Homebrew from VSCode's terminal. Brew is a Package Manager for MacOS that can install neat and necessary things.

  3. (MacOS only) Go to Microsoft for explicit additional brew commands for set up (.net core 1.x). Dotnet Core 1.x requires OpenSSL to work on MacOS. You can pretty much forget about Brew now.

  4. Navigate to your project folder: cd ~/Source/my-nuget-project

  5. Make a directory for the NuGet package source of your project, i.e. mkdir src

  6. Open the folder we just created (src/) in VSCode. File > Open Folder

  7. Hit Control + ` to open the terminal.

  8. Exec the following in your VSCode terminal:

    dotnet new classlib --name [yourUniqueClassLibName]
    

Great. You've created a blank class library, and it’s definitely something someone else could totally use. Let’s

git commit -m "my class library initial commit"

Pack and Push

Onto the meaty part: getting your commands right for packing your NuGet package and pushing it up to nuget.org.

cd into your projects src/ directory

cd src/
dotnet restore
dotnet build -c release
dotnet pack -c release
ls bin/release/

Neat. Ya packed it. Dotnet makes this all so very simple. NuGet is obviously a big part of the backbone to Dotnet Core so they probably wanted to make it this easy. Next, go to nuget.org and create an account, which is actually a Microsoft account. Once you’re all logged in, go to your profile, scroll to the bottom, and find the API Keys section. Click the button to generate a new API key. This is where it’ll ask you to confirm your account. Once you've confirmed it, then get back to the same screen, and click to generate a new API key.

  1. It will prompt you to add a descriptive name for the key the key I use for my awesome new NuGet project
  2. Check both Push and Unlist package checkboxes.
  3. Type * in the Glob pattern field, 'cause hey, let’s be safe.
  4. Hit Add Key
  5. Click the icon to copy the key. This is your last chance to do this, so don't close the window yet.

Ok, now we have the key we need to push your NuGet package to nuget.org. Open the terminal in vscode. cd ../ then pwd to confirm you’re in your projects root directory. Then execute

touch my-nuget-key.md

Open up the file we just created in vscode, paste your key into it, and hit save. Go back to your terminal and execute:

touch .gitignore

Open your .gitignore file and add the line

my-nuget-key.md

Then execute

git add *; git add .gitignore; git commit -m "don’t show anyone my key not never!"

Cool. I think we’re ready for the last and best steps. Navigate to your release bin folder in your terminal, bsh cd src/bin/Release then execute

dotnet nuget push *.nupkg --api-key [guid] --source "https://www.nuget.org/api/v2/package"

BAM! You should be done. You created some code that other people can

dotnet add package [your package name]

Super simple. Now you’re ready to change the way people code with an awesome new open source project!

Rich Minchuk

Technology Enthusiast and Wannabe Growth Hacker