Skip to content

A template repository of C# nuget package base structure

Notifications You must be signed in to change notification settings

mfdeveloper/CsharpPackageBase

Repository files navigation

ComponentBase Component/Library

This is a nuget/paket main scaffold component to create others packages

Requirements

  • Build and run tests

  • .NET official package manager to generate .nupkg file

  • Integrated with Nuget to manage dependencies, with more clear files and allow use Git dependencies

Getting Started

1. Install .NET Core

2. Install Nuget

Use Chocolatey

choco install nuget.commandline

Optionally, you can install with Chocolatey too

# Install nuget AUTH PROVIDER
# to push package for feed authenticate
choco install nuget-credentialprovider-vss

This is required to push .nupkg packages to Azure Artifacts

3. Install Paket

Use .NET Core tools to install paket command line locally

dotnet tool install -g paket

If you got any error using paket with .NET Core, try install using chocolatey

choco install paket

PS: Install globally if you will use paket in others projects. You can use globally also if your project not use .NET Core.

3.1 Install Azure pipelines CLI (OPTIONAL)

Use Chocolatey

# Install base Azure CLI (command line)
choco install azure-cli

# Install Azure Devops extensions
az extension add --name azure-devops

4. Install all DEPENDENCIES and restore references

paket restore
dotnet restore

5. Build and test the solution

# Build the solution
dotnet build

# Run all tests
dotnet test

Project structure

Your project folders structure should be:

component-directories-explanation

Generating package (.nupkg)

  1. Make sure that project builds and all tests are passed

    # Build the solution
    dotnet build
    
    # Run all tests
    dotnet test
  2. Generate the .nupkg file

    # Pack your component to the path nupkg/*.nupkg
    dotnet pack --include-symbols --configuration Release --output .\nupkg [your-component-project]

    PS: If you not pass the --outputs flag, the .nupkg file is generated in: [your-component-project]/bin/Release by default

    Or use Visual Studio and right click on your project component under Solution explorer, and click Pack.

    component-pack-nupkg

    Error "Pack Task"

    If you got a Pack task error with .NET Core CLI or in Visual Studio, use Paket pack command:

    paket pack --template [your-component-project]\[your-component-project].csproj.paket.template --build-config Release --symbols .\nupkg

Testing your package locally

Method 1: Use paket.local file (recommended)

The preferrable option is create a paket.local file in your consumer project/application, and add the source path of your package. The source property could be a path that contains a .nupkg file. In this case, you need build and pack your component first:

- Build/Pack your component project first

# Use Visual Studio: Build => Build Solution
# or use dotnet core CLI
dotnet build

- Create the paket.local file in your consumer root solution project/application

paket.local

nuget [my-component-id] -> source [my-component-solution]\[my-component-project]\[nupkg-folder] version [my-component-version]

For more information, see the official documentation: The paket.local file

Method 2: Create a local feed "server"

  1. After generated the .nupkg file, create a folder accessible to you (local directory or network share) as package repo:

    Publish the package to local repo using nuget add:

    nuget add [my-component.{version-number}].nupkg -source [your/local-repo/directory]

Add the package/component to a application

Using Paket

  1. Add your local folder like a source to the paket.dependencies file, in a project/application that will use this package like a dependency:

     source D:\source\nuget_repo
     source ~/source/nuget_repo
     source \\server\nuget_repo
    
     # Paket supports relative directory to the paket.dependencies too
     source directory/relative/to/paket.dependencies

    PS: Pay attention to avoid commit this file with the my/local/path to your CVS repository (like git). This is only to local tests.

    If you wish add a commit with the hardcoded path, add a common directory for everyone, for example: C:\Program Files\Microsoft SDK\NuGetLocal

  2. Add the packageId like a dependency into paket.dependencies file of your application:

    component-packageid-dependency

Publish to Azure Devops/Artifacts

Using Nuget

  1. If your destiny project/application uses only Nuget like a package manager (without Paket), you can add the nuget repo folder mentioned above with Visual Studio:

    component-local-source-folder

    For more information, see these tutorials:

  2. Restore the project to provide authentication

    nuget restore
  3. Push to the Azure Artifacts feed

    # See the nuget.config file <packageSources> tag
    nuget push -Source "[your-feed-name]" -ApiKey az [path/to/nupkg-file].nupkg

Install remote package to an application

Using Paket

  1. Add your remote feed URL like a source to the paket.dependencies file, in a project/application that will use this package like a dependency:

     # [organization]: Your organization in Azure Devops
     # [feed]: Your feed/server name, created in Azure Artifacts
     source https://pkgs.dev.azure.com/[organization]/_packaging/[feed]/nuget/v3/index.json
    
     # Paket supports relative directory to the paket.dependencies too
     source directory/relative/to/paket.dependencies
  2. Add the packageId like a dependency into paket.dependencies file of your application:

# [YourAppProject]: Is project name inside of the solution, that to use this dependency like a reference

## Using dotnet core
paket add --project [YourAppProject]/[YourAppProject].csproj {YourComponent.PackageIdName}

## Using .NET Framework
.\.paket\paket.exe add --project [YourAppProject]/[YourAppProject].csproj {YourComponent.PackageIdName}

As result will be added in the paket.dependencies file:

component-packageid-dependency

And in the [YourComponent]/paket.references file:

component-packageid-reference

Using Nuget

  1. If your destiny project/application uses only Nuget like a package manager (without Paket), you can add the remote feed URL mentioned above with Visual Studio:

    component-remote-source

    Where:

    • [my-organization]: The name of the remote feed/server
    • [organization]: replace with your organization name defined in Azure Devops
    • [feed]: replace with your private feed (by organization or project) where the package that will be used like an dependency was uploaded.

    For more information, see this official Microsoft tutorial:

  2. Add the packageId like a dependency into package.config or using the tag <PackageReference> in the [YourComponent]\YourComponent.csproj XML file. You can do this using Visual Studio:

    visual-studio-nuget-packages

Right click on References under the component project => Manage Nuget Packages

  1. On the next screen, search by the packageId and click on Install button

Problems and workarounds

See the wiki pages in: CsharpPackagetBase Wiki

About

A template repository of C# nuget package base structure

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages