To distribute a custom project structure as a reusable item, you need to configure the template metadata and package it for NuGet distribution. The following steps outline this process.
1. Configure Template Metadata
Create a directory named .template.config at the root of your project. Inside this directory, add a file named template.json to define the template's behavior and properties.
{
"$schema": "http://json.schemastore.org/template",
"author": "DevTeam",
"classifications": [ "Solution", "Web" ],
"name": "Microservice Scaffold",
"identity": "Microservice.Scaffold.Template",
"shortName": "micro-svc",
"tags": {
"language": "C#",
"type": "project"
},
"sourceName": "MyScaffoldName",
"preferNameDirectory": true
}
2. Prepare the NuGet Specification
Structure you're source files for packaging. It is common practice to place the project folder inside a directory (e.g., named Source) that sits alongside your build artifacts. In this parent directory, create a .nuspec file to define the package metadata.
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>Microservice.Scaffold</id>
<version>2.0.1</version>
<description>A foundational template for building RESTful microservices.</description>
<authors>DevTeam</authors>
<packageTypes>
<packageType name="Template" />
</packageTypes>
</metadata>
</package>
3. Package and Publish
Download the nuget.exe CLI tool from the official distribution site and place it in your working directory.
Packaging
Execute the following command in your terminal (using PowerShell or CMD) to generate the .nupkg file based on your specification:
./nuget.exe pack Microservice.nuspec -OutputDirectory ./dist
Note: You can verify the contents of the generated package using a tool like NuGetPackageExplorer, though this step is optional.
Publishing
Push the generated package to your private NuGet feed using the command below:
./nuget push Microservice.Scaffold.2.0.1.nupkg -Source "https://your-private-feed.com/v3/index.json"
Installatino
To make the template available in your local development environment, install it directly from the source:
dotnet new install Microservice.Scaffold --nuget-source "https://your-private-feed.com/v3/index.json" --force
Verification
Confirm that the template is installed by listing all available templates:
dotnet new list
You can also open Visual Studio, create a new project, and search for the template by name.
Usage
Instantiate a new project based on your template in an empty directory using the short name defined in the configuration:
dotnet new micro-svc -n MyNewService
Uninstalling
If you need to remove the template to update it or clean up your environment, use the uninstall command:
dotnet new uninstall Microservice.Scaffold