Building a reusable .NET NuGet package
Copy-pasted helper code becomes expensive quickly. A shared text parser, validation routine, or HTTP client may look trivial in one repository, yet every copy needs its own fixes, security checks, and tests. Moving that capability into a versioned .NET library gives applications one implementation and creates a deliberate upgrade path instead of hidden drift.
A NuGet package is more than a zipped DLL. It is a public contract with package metadata, dependencies, documentation, symbols, and a licence. Consumers should be able to install it, understand what it does, and receive sensible upgrades without rebuilding their applications. The package must also remain maintainable after its first release.
These needs are familiar to development teams in Sydney, Melbourne, and Brisbane, while distributed teams may also include colleagues in Perth on Australian Western Standard Time. Package documentation should use Australian English and clearly identify dates as day, month, and year. Public packages reach the Australian market through NuGet.org, while many organisations combine that registry with an internal Azure Artifacts feed.
The central discipline is to treat the library as a product, with clear boundaries, deliberate compatibility, reproducible builds, and verified releases. When those practices are in place, reuse scales across repositories without turning each consumer into an unsupported branch of the original code.
Define the package boundary
Begin with the capability rather than the organisation chart. A package called Company.Common containing unrelated utilities tends to attract accidental dependencies and becomes difficult to deprecate. A focused library such as Acme.TextTools can state its purpose clearly, receive an appropriate name and version, and evolve without affecting unrelated projects.
A practical repository can contain a solution file, a src directory for production libraries, a tests directory, and a samples directory containing a real consumer. Keep the main project as an SDK-style .csproj file. Shared build settings belong in a .props or .targets file when several libraries genuinely need them, but the package’s identity and metadata should remain visible in its own project.
Treat the public API as a contract. Expose types and methods that solve the advertised problem, document supported behaviour, and make invalid states difficult to construct. Consider nullable reference types, CancellationToken support for asynchronous operations, exception guarantees, and compatibility with dependency injection. Dependencies should support the capability directly rather than being included merely because another internal service happens to use them.
Configure metadata and build output
The project file is the source of truth for the package identity. A minimal library might use the following configuration:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<PackageId>Acme.TextTools</PackageId>
<PackageVersion>1.0.0</PackageVersion>
<Authors>Acme Engineering</Authors>
<Description>Text parsing and validation utilities for .NET applications.</Description>
<PackageTags>text;validation;parsing</PackageTags>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>
</Project>
Keep PackageId stable because consumers will reference it in project files, lock files, and deployment documentation. The description should explain the problem solved rather than repeat the package name. Ensure the declared licence matches the repository licence, and pack the README so it appears on the NuGet.org listing. Use TargetFrameworks instead of TargetFramework when several runtime targets are supported.
Restore, build, test, and pack through the same commands used by continuous integration:
dotnet restore Acme.TextTools.sln
dotnet build Acme.TextTools.sln -c Release
dotnet test Acme.TextTools.sln -c Release
dotnet pack src/Acme.TextTools/Acme.TextTools.csproj -c Release -o artifacts
The result should be a .nupkg containing the intended assemblies, dependency declarations, metadata, README, and licence. A separate .snupkg can carry symbols for debugging. Inspect the archive before release to ensure it contains no credentials, test files, source-control metadata, or unnecessary content.
Manage versions and dependencies
Use semantic versioning consistently. A compatible feature or fix can produce 1.2.0, a breaking API change requires a new major version such as 2.0.0, and experimental releases can use labels such as 2.0.0-beta.1. Never reuse a published version on NuGet.org, because immutability gives consumers confidence that the same version always resolves to the same artefact.
Choose target frameworks from actual consumer demand rather than a desire to appear comprehensive. Supporting net8.0 gives applications on the long-term-support .NET release a straightforward option, while another target can be added when its support window, dependencies, and testing matrix justify it. Declare only direct runtime dependencies. For a dependency with version 1.2.3, NuGet normally treats it as an inclusive minimum and resolves an applicable higher version, so state your compatibility expectations carefully and test against realistic dependency graphs.
Public API compatibility needs an automated gate. Removing a method, changing its parameter type, or altering a return contract can break source or binary compatibility even when the library’s internal behaviour appears unchanged. The .NET SDK can validate a package against a baseline and flag incompatible API changes. Additional background is available in package compatibility guidance. Record intentional removals in a changelog and explain migration steps before publishing the version that introduces them.
Test the artefact as a consumer
Unit tests verify the library’s behaviour, but they do not prove that the packed package can be restored by another project. Maintain a small consumer application that references the library through PackageReference. This catches missing runtime assets, incorrect dependency declarations, broken namespaces, and packaging rules that a project-to-project reference can hide.
Publish the candidate .nupkg to a local folder and install it into the consumer:
dotnet add package Acme.TextTools \
--version 1.0.0 \
--source ../artifacts
dotnet restore --no-cache
dotnet run --no-restore
Use a clean package cache or an isolated restore so an older build cannot satisfy the test accidentally. Run the sample on supported operating systems and runtime versions, then inspect transitive dependencies with dotnet list package --include-transitive. A package that works when referenced directly can still fail if it accidentally exposes a development-only dependency to consumers.
Also examine what users receive inside the .nupkg. Confirm that runtime-specific folders are correct, XML documentation and the README are present, and debug symbols remain in the separate symbol package. Automated package validation and consumer smoke tests should run in CI for every proposed release, not only when someone prepares a manual publication.
Publish and maintain the release
A typical pipeline builds and tests pull requests, creates the package without publishing it, and attaches the artefact for review. A protected release job can then promote the exact tested package after approval. Store any NuGet.org API key in the CI provider’s encrypted secret store, restrict it to the publishing environment, and never print it in build output. The publishing identity must also have permission to push the chosen package identifier.
Public distribution through NuGet.org gives a small Australian library access to consumers domestically and internationally. Larger Australian organisations may mirror approved packages through Azure Artifacts or require an internal source for intellectual-property reasons. If the project remains free and open source, there is no package sale to tax; a commercial licence can introduce Australian GST obligations, including the standard 10% rate when the business is registered and the transaction falls within scope.
Publication creates an ongoing support commitment. Publish a changelog, security contact, supported runtime matrix, and expected maintenance period. Where Australian teams follow local change-management customs, name an accountable release owner and state any deployment cut-off in the relevant local time zone. Avoid unqualified dates in release notes by writing 04/07/2025 (day/month/year) when the distinction matters.
Release practices that prevent package drift
These practices create a dependable path from source code to a package consumers can trust:
- Keep the package identifier stable and reserve it in the relevant registry before promotion.
- Publish only the exact artefact that passed build, test, validation, and consumer smoke tests.
- Apply semantic versioning and document every breaking change with migration guidance.
- Test the packed
.nupkgfrom a clean consumer project on every supported target. - Automate releases through protected CI credentials and an auditable approval step.
- Ship a README, changelog, licence, support policy, and symbol package with every stable release.
Create a small pilot repository today with one library, one consumer sample, and a CI release workflow. Publish version 1.0.0 only after the package has passed the consumer test, then ask a colleague in another Australian office to restore it. That first cross-repository installation is the practical starting point for a dependable internal ecosystem.