skip to Main Content

I’ve been trying to create a .NET template for a Web API project with DDD architecture and publish it to GitHub Packages. Although I’ve configured the .nuspec, .csproj, and template.json files to define it as a template, when I query the GitHub NuGet feed, the template is treated as a standard package instead of being recognized as a dotnet-new template.

Here are the details of my setup:

csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <PackageId>MyCompanyTemplate</PackageId>
    <Title>MyCompany Template (Web Api DDD)</Title>
    <Authors>MyCompany S.r.l.</Authors>
    <Description>Template for Web Api DDD</Description>
    <PackageTags>dotnet-new;template;webapi;ddd;mycompany</PackageTags>
    <TargetFramework>net8.0</TargetFramework>
    <ProjectType>Template</ProjectType>
    <RepositoryUrl>https://github.com/mycompany/mycompany</RepositoryUrl>
    <PublishRepositoryUrl>https://nuget.pkg.github.com/mycompany</PublishRepositoryUrl>
    <NuspecFile>MyCompanyTemplate.nuspec</NuspecFile>

    <IncludeContentInPack>true</IncludeContentInPack>
    <IncludeBuildOutput>false</IncludeBuildOutput>
    <ContentTargetFolders>content</ContentTargetFolders>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="src***;test***;*.sln;.template.config**" Exclude="src**bin**;test**bin**" />
    <Compile Remove="***" />
  </ItemGroup>

</Project>

nuspec

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
  <metadata>
    <id>MyCompanyTemplate</id>
    <title>ASP.NET Core Web Api template</title>
    <version>0.0.1</version>
    <authors>MyCompany S.r.l.</authors>
    <description>
      First version of a web api template.
    </description>
    <language>it-IT</language>
    <license type="expression">MIT</license>
    <projectUrl>https://github.com/mycompany/MyCompanyTemplate</projectUrl>
    <releaseNotes>
* FIRST RELEASE!

    </releaseNotes>
    <packageTypes>
      <packageType name="Template" />
    </packageTypes>
    <tags>Web ASP.NET</tags>
  </metadata>
  <files>
    <file src=".**" target="content" exclude=".bin**;.obj**;..git**;..github**;..user;..vs**;..vscode**;..idea**" />
  </files>
</package>

template.json

{
  "$schema": "http://json.schemastore.org/template",
  "author": "MyCompany S.r.l.",
  "classifications": ["Web", "DDD", "Api", "MyCompany"],
  "identity": "MyCompanyTemplate",
  "name": "MyCompany Template",
  "shortName": "mycompanytemplate",
  "tags": {
    "language": "C#",
    "type": "solution"
  },
  "sourceName": "MyCompanyTemplate",
  "preferNameDirectory": true,
  "sources": [
    {
      "modifiers": [
        {
          "exclude": [
            ".vs/**",
            ".template.config/**",
            ".git/**",
            "./.DS_Store",
            ".idea/**",
            "./Template.csproj",
            "./global.json",
            ".github/**",
            "README.md",
            "./**/bin/**",
            "./**/obj/**",
            "nupkgs/**"
          ]
        }
      ]
    }
  ]
}

publish.yml for github actions

name: Publish Template

on:
  push:
    branches:
      - master
  workflow_dispatch:

jobs:
  build-and-publish:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Setup .NET
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: "8.0"

      - name: Restore Dependencies
        run: dotnet restore MyCompanyTemplate.csproj

      - name: Build Template
        run: dotnet pack MyCompanyTemplate.csproj --configuration Release

      - name: Publish to GitHub Packages
        run: dotnet nuget push bin/Release/*.nupkg -k ${{ secrets.SECRET }} -s https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json

PUBLISHED PACKAGE

Why is my .NET CLI template being recognized as a regular NuGet package when published to GitHub Packages? Is there something wrong with the .csproj, .nuspec, or template.json configuration? How can I fix this so the template is properly identified by dotnet new commands?

Any help or guidance would be greatly appreciated!

2

Answers


  1. As far as I can see github does not distinguish between the templates projects and "ordinary" Nuget ones:

    enter image description here

    enter image description here

    You can try publishing them to https://www.nuget.org/, but in general this should not be a problem – try installing the published template from Github.

    Login or Signup to reply.
    1. packageType in .nuspec
      The packageType tag in your .nuspec is correct:

      <packageTypes>

      `

    This ensures the NuGet package is classified as a template package. However, you may also need to double-check the version of the Template package type in use. The most compatible setting is:

    <packageType name="Template" version="1.0.0" />
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search