Azure DevOps打包自定义项目模板后安装提示未找到模板,求解决方案
问题:自定义项目模板安装提示“No templates were found in the package”
摘要
安装打包后的自定义项目模板时,收到如下错误:
No templates were found in the package MyTemplate.Domain::1.0.1.
背景
我创建了一个类库项目作为DDD领域层的模板,相关信息如下:
项目结构
sln [MyTemplate.Domain] |_MyTemplate.Domain (= class lib project) |_.template.config | |_template.json |_interfaces | |_IManageBooks.cs |_Book.cs |_README.md
核心代码
IManageBooks.cs
public interface IManageBooks { void ReleaseForRent(Book book); void Archive(Book book); }
Book.cs
public class Book { public string Name { get; set; } = default!; public string Author { get; set; } = default!; public int Pages { get; set; } = default!; public string Status { get; set; } = default!; public void Release() { this.Status = "ACTIVE"; } public void Archive() { this.Status = "ARCHIVE"; } }
模板配置
.template.config/template.json内容:
{ "$schema": "http://json.schemastore.org/template", "author": "Me", "classifications": [ "Template", "C#", "DDD", "Domain" ], "identity": "MyTemplate.Domain", "name": "MyTemplate Domain Project Template", "shortName": "mytemplate-domain", "sourceName": "mytemplate-domain", "tags": { "language": "C#", "type": "solution" }, "preferNameDirectory": true }
Azure DevOps流水线配置
trigger: - main pool: vmImage: 'windows-latest' name: 1.0.$(Rev:r) variables: solution: '**/*.sln' buildPlatform: 'Any CPU' buildConfiguration: 'Release' steps: - task: NuGetToolInstaller@1 - task: NuGetCommand@2 inputs: command: 'restore' restoreSolution: '$(solution)' feedsToUse: 'config' nugetConfigPath: './nuget.config' - task: VSBuild@1 inputs: solution: '$(solution)' msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"' platform: '$(buildPlatform)' configuration: '$(buildConfiguration)' - task: DotNetCoreCLI@2 displayName: dotnet pack inputs: command: 'pack' packagesToPack: '**/MyTemplate.Domain.csproj' versioningScheme: 'byBuildNumber' - task: DotNetCoreCLI@2 displayName: dotnet push inputs: command: 'push' packagesToPush: '$(Build.ArtifactStagingDirectory)/*.nupkg' nuGetFeedType: 'internal' publishVstsFeed: 'a28b3df0-f844-48da-b710-8b028b28a868/75bb5107-aab1-43be-aea6-138e7583753d' - task: PublishBuildArtifacts@1 displayName: publish artifacts inputs: PathtoPublish: '$(build.artifactStagingDirectory)' TargetPath: '\\MyTemplate.Domain.Templates\$(Build.DefinitionName)\$(Build.BuildMyTemplateDomainTemplates)' publishLocation: 'Container'
流水线运行无报错,但执行以下安装命令时:
dotnet new install mytemplate.domain --interactive --nuget-source https://pkgs.dev.azure.com/my-company/a28b3df0-f844-48da-b710-8b028b28a868/_packaging/41db8e76-ca11-7368-90bb-3f9dd37c4590/nuget/v3/index.json
得到输出:
The following template packages will be installed: mytemplate.domain No templates were found in the package mytemplate.Domain::1.0.1.
疑问
我已经完成模板创建、配置、打包推送的流程,是否遗漏了关键步骤?该如何正确安装这个模板包?
解决方案
1. 确保模板配置文件被纳入NuGet包
打开MyTemplate.Domain.csproj,添加或确认以下配置,保证.templateconfig文件夹及内部文件被打包进NuGet:
<ItemGroup> <Content Include=".templateconfig\**"> <Pack>True</Pack> <PackagePath>\</PackagePath> </Content> </ItemGroup>
如果缺少这段配置,打包时模板配置不会被包含,导致dotnet new无法识别。
2. 修正VSBuild任务参数
你的VSBuild任务使用了Web项目专属的部署参数(/p:DeployOnBuild=true等),对于类库模板项目完全多余,简化为:
- task: VSBuild@1 inputs: solution: '$(solution)' platform: '$(buildPlatform)' configuration: '$(buildConfiguration)'
多余参数可能干扰类库的正常编译和打包流程。
3. 验证NuGet包内容
从Azure DevOps包管理界面下载已推送的NuGet包,解压后检查是否包含.templateconfig文件夹和template.json文件。如果没有,说明打包时未正确包含文件,回到第一步排查项目配置。
4. 搜索模板包验证可用性
安装前先执行以下命令搜索模板,确认包能被识别:
dotnet new search mytemplate.domain --nuget-source https://pkgs.dev.azure.com/my-company/a28b3df0-f844-48da-b710-8b028b28a868/_packaging/41db8e76-ca11-7368-90bb-3f9dd37c4590/nuget/v3/index.json
如果能搜到,说明包本身没问题,安装问题可能来自缓存或权限;搜不到则继续排查包结构。
5. 清理本地模板缓存
若之前安装过同名模板,可能存在缓存冲突,执行以下命令清理后重新安装:
dotnet new uninstall mytemplate.domain dotnet new install mytemplate.domain --interactive --nuget-source <你的源地址>
内容的提问来源于stack exchange,提问作者kbd
相关产品推荐
相关产品推荐

