Create And Publish A Nuget Package Using Visual Studio (.NET Standard)

Create a class library project

You can use an existing .NET Standard Class Library project for the code you want to package or create a simple one as follows:

Step 1

In Visual Studio, choose File > New > Project, expand the Visual C# > .NET Standard node, select the "Class Library (.NET Standard)" template, name the project ownNuGet, and click OK.

.NET Standard

Step 2

Once your project is ready, open Solution Explorer, right-click “project”, and click on “Add” and add "Class".

.NET Standard 

Add "Class" and set a name for your class as you want for your Project and click on “Add” button.

 .NET Standard
Step 3

Write your program in class and create your function for global access.

.NET Standard

Configure package properties

Select the Project > Properties menu command, then select the Package tab. (The Package tab appears only for .NET Standard class library projects; if you are targeting .NET Framework.)

.NET Standard 

Give your package a unique identifier and fill out any other desired properties.

Step 4 - Run the pack command

  • Set the configuration to Release
  • Right click the project in Solution Explorer and select the Pack command:
.NET Standard 
  • Visual Studio builds the project and creates the .nupkg file. Examine the Output window for details (similar to the following), which contains the path to the package file. 
 .NET Standard
Step 5 - Publish the package

Once you have a .nupkg file, you publish it to nuget.org using either the nuget.exe CLI or the dotnet.exe CLI along with an API key acquired from nuget.org.

Acquire your API key
  1. Sign into your nuget.org account or create an account if you don't have one already.
  2. Select your username (on the upper right), then select API Keys.
  3. Select Create, provide a name for your key, select Select Scopes > Push. Under API Key, enter * for Glob pattern, then select Create. (See below for more about scopes.)
.NET Standard 
 
Once the key is created, select Copy to retrieve the access key you need in the CLI,

.NET Standard

Note

Save your key in a secure location because you cannot copy the key again later on. If you return to the API key page, you need to regenerate the key to copy it. You can also remove the API key if you no longer want to push packages via the CLI.

Step 6 - Publish with nuget push

For publish on nuget.org firstlyv download NuGet Distribution Versions https://www.nuget.org/downloads

.NET Standard 
 
 When a download is done open file explorer and set it in your system drive and copy location of NuGet application.
.NET Standard

Step 7

Now, right click on your PC and click on “properties”, you will see a control panel, now click on “advanced system setting” and a “system properties” popup will open. Then click on “Environment Variables”, a popup will open ,then click on “Path” and edit.

.NET Standard
 An edit Environment Variables popup will open, now click on new and paste the location of the NuGet application and click on ‘Ok’.

.NET Standard

Step 8

Now, open “CMD” and write “NuGet” for available commands. See the below CMD screen.

.NET Standard

 

Now, copy the location of your “OwnNuGet.1.0.0” in /bin/debug and set location in “cmd” and run the following command, specifying your package name and replacing the key value with your API key:
  1. dotnet NuGet push OwnNuGet.1.0.0.nupkg oy2gjga8pl3dvn2akksyquwcs9ygggg4exypy3bhxy6w6x –s https://api.nuget.org/v3/index.json
dotnet displays the results of the publishing process:

.NET Standard

Step 9

Now, open https://www.nuget.org and go to your account and you will see your package name.

.NET Standard

 

Now, click on your package name and you will see the screen as below with Installation package.
 
.NET Standard

Congratulations, your package is successfully pushed on NuGet.

In the next article, I will explain how to get and use our package.

Thanks.


Similar Articles