Skip to main content

Create Custom Nuget Package Via Visual Studio & Publish

1. Creating Your Project

  1. Create a class library project

  2. Open Visual Studio, Create New Project

  3. Select Class Library for .NET

    • Type c# class library in search bar

    • From dropdown select Class Library (NOT class library for .NET Framework)

      Untitled

  4. Name the Project & Solution Name (e.g. Agate.NetCore.RnD.MySql.Core). See Naming standard [here](Agate Nuget Project Name & Namespaces Standards b569a74e74d445158fdc71c25464e8c9.md)

    Untitled

  5. Select .NET Version corresponded with your project (ex: .NET 6.0) & Create

    Untitled

  6. Create Your Logic/classes

  7. Configure packages general properties

    1. Right click on project > select Properties at very bottom

      Untitled

    2. In Application > General, changes the value of Assembly Name & Default Namespace to your package name

      Untitled

    3. Open the .csproj file (double click on root project in hierarchy) & you would see this on PropertyGroup Section

      	<PropertyGroup>
          <TargetFramework>net6.0</TargetFramework>
          <ImplicitUsings>enable</ImplicitUsings>
          <Nullable>enable</Nullable>
          <AssemblyName>Agate.Netcore.Mysql.Repository.Core</AssemblyName>
          <RootNamespace>Agate.Netcore.Mysql.Repository.Core</RootNamespace>
        </PropertyGroup>
      
    4. Save the file

2. Build & Package Project

  1. Before we build and packing the project, we need to enable auto generate .nupkg file on build
  • Right click on project > select Properties at very bottom

  • In Package section > General

  • Set Generate NuGet package on build checkbox to true (checked)

  • in .csproj file, you should see one property appeared in PropertyGroup

    <PropertyGroup>
    	...	
      <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
    </PropertyGroup>
    
  • Back to Properties > Package > General, let give the details of our package

    Property Name Example Value

    | PackageID | Agate.Netcore.Mysql.Repository.Core | | PackageVersion | 1.0.1 | | Authors | Agate Epic Teams or you can give your own name like Bima, Billy | | Company | Agate International | | Description | MySql base repository for .net core 6.0 | | RepositoryURL | https://gategit.agate.id/tech/agate-open-source/backend-template/game-backend-template | | Release Notes | Initial release specifies the changes of this release |

  • in .csproj file, new properties appeared in the PropertyGroup

    <PropertyGroup>
    	<TargetFramework>net6.0</TargetFramework>
    	<ImplicitUsings>enable</ImplicitUsings>
    	<Nullable>enable</Nullable>
    	<AssemblyName>Agate.Netcore.MySql.Repository.Core</AssemblyName>
    	<RootNamespace>Agate.Netcore.MySql.Repository.Core</RootNamespace>
    	<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
    	<PackageId>Agate.Netcore.MySql.Repository.Core</PackageId>
    	<Version>1.0.0</Version>
    	<Authors>Agate Team</Authors>
    	<Company>Agate International</Company>
    	<Product>Agate.Netcore.MySql.Repository.Core</Product>
    	<Description>My SQL base repository for .net core 6.0</Description>
    	<RepositoryUrl>https://gategit.agate.id/tech/agate-open-source/backend-template/game-backend-template</RepositoryUrl>
    </PropertyGroup>
    
  • Save the .csproj file

  1. Run the pack command
    • In Build menu > select Pack
    • Take a look in output window
  2. After it succeed, go to the project folder in explorer > bin > Debug
  3. You will see .nupkg file is created

3. Push To Gitlab

  1. Create repository on Agate Gitlab
  2. Set RepositoryUrlin csproj file and refer to your url gitlab.
  3. Push on Gitlab

4. Publish into Agate NuGet

  1. Open CLI in where the .nupkg file is located (project root/bin/debug)

  2. Run command

    dotnet nuget push -s https://nuget-global.agate.id/v3/index.json -k <NUGET-SERVER-KEY> Agate.Netcore.MySql.Repository.Core.1.0.0.nupkg
    

    Notes: Change into the key on [Agate Internal NuGet Feed](Agate Internal NuGet Feed 2b3ca56457274846aab2b7a0871ae73b.md) .

  3. Your package is now up in https://nuget-global.agate.id/

Reading References:

Quickstart: Create and publish a NuGet package using Visual Studio (Windows only)

Nuget Template Package (For ASP .NET)