How to Test NuGet Packages Locally Before Publishing to NuGet Feed

KUMAR BISHOJITKUMAR BISHOJIT
2 min read

When developing NuGet packages, testing them in real projects before official publication is crucial. Here's a comprehensive guide to setting up local testing:

STEP-1: Configure Local Package Source

Add to your .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>net9.0</TargetFramework>
        <!-- Add local package source -->
        <RestoreAdditionalProjectSources>
            $(RestoreAdditionalProjectSources);
            D:\P_Bikiran\Bikiran.Validation\bin\Release\
        </RestoreAdditionalProjectSources>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Bikiran.Validation" Version="1.0.7" />
    </ItemGroup>
</Project>

2. Package Development Workflow

  1. Build your package

    Ensure your package project is built in the Release configuration

     dotnet build -c Release
    
  2. Generate a .nupkg file

     dotnet pack --configuration Release
    
  3. Verify package structure

    Confirm the .nupkg file exists in your output directory:

     D:\P_Bikiran\Bikiran.Validation\bin\Release\Bikiran.Validation.1.0.7.nupkg
    

3. Consumption in Live Projects

using Bikiran.Validation; // Your local package
using Bikiran.Utils.ApiResp;

public class DomainCnsAddProperty
{
    public string Cns { get; set; } = "";
    public List<string> Ips { get; set; } = [];

    public ApiResponse Validate()
    {
        var validationResult = ValidateBasic.ValidateAll([
            ValDomain.IsValidDomainFormat(Cns, "Child Name Server"),
            ValIP.IsValidIpFormatAll(Ips, "IP Addresses"),
        ]);

        return new ApiResponse {
            Error = validationResult.Error,
            Message = validationResult.Message,
            ReferenceName = GetReferenceName(validationResult.ErrorIndex)
        };
    }

    private string GetReferenceName(List<string> names, int? errorIndex)
        => errorIndex.HasValue ? names[errorIndex.Value] : "Validation";
}

Best Practices

  1. Version Management

    • Increment versions systematically (SemVer recommended)

    • Use wildcards for development versions: 1.0.*

  2. Dependency Isolation

     <PackageReference Include="Bikiran.Validation" Version="1.0.7">
       <PrivateAssets>all</PrivateAssets>
     </PackageReference>
    
  3. CI/CD Integration

    Consider adding a local package directory in your build pipeline:

     <RestoreAdditionalProjectSources>
       $(RestoreAdditionalProjectSources);
       $(Build.SourcesDirectory)\artifacts
     </RestoreAdditionalProjectSources>
    

Troubleshooting Common Issues

  1. Package Not Found

    • Verify the .nupkg file exists at the specified path

    • Check package version consistency

    • Clear NuGet caches: dotnet nuget locals all --clear

  2. Version Conflicts

    Use exact versions during development:

     <PackageReference Include="Bikiran.Validation" Version="[1.0.7]" />
    
  3. IntelliSense Issues

    • Restart IDE after package updates

    • Run dotnet restore manually

CONTACT:

I’m Kumar Bishojit Paul, the Founder and CEO of BIKIRAN. If you need further assistance, please leave a comment. I’m interested in helping you.

0
Subscribe to my newsletter

Read articles from KUMAR BISHOJIT directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

KUMAR BISHOJIT
KUMAR BISHOJIT