I have a system where users create .NET plugin libraries. To support this, I’ve built:
A
solutiontemplate that creates:- a plugin project
- a testbed console app
- a project reference from the testbed → plugin
A
projecttemplate that allows adding additional plugin projects to the same solution.
After running the solution template, the structure looks like:
AllMyPlugins/
|-- AllMyPlugins.sln
|-- Testbed/
| |-- Program.cs
| |-- Testbed.csproj <-- already references MyXxxPlugins
|
|-- MyXxxPlugins/
|-- Module1.cs
|-- MyXxxPlugins.csproj
If the user then adds another plugin using the project template:
AllMyPlugins/
|-- AllMyPlugins.sln
|-- Testbed/
| |-- Program.cs
| |-- Testbed.csproj <-- still references MyXxxPlugins,
| but not MyYyyPlugins
|-- MyXxxPlugins/
| |-- Module1.cs
| |-- MyXxxPlugins.csproj
|
|-- MyYyyPlugins/
|-- Module1.cs
|-- MyYyyPlugins.csproj
The question
Is it possible for a project template (instantiated from Visual Studio, not from dotnet new) to automatically add a project reference to an existing project in the solution?
Specifically:
Can the template modify the already-existing Testbed project so that it references the newly created plugin project?
My tests were done using:
- VS 2022 (17.14.20)
- VS 2026 (18.0.0)
Template configuration (reduced)
Here is the reduced template.json:
{
"$schema": "http://json.schemastore.org/template",
"identity": "Abc.Prj.cs",
"name": "ABC Project",
"shortName": "abc-prj-cs",
"tags": {
"language": "C#",
"type": "project"
},
"sourceName": "NewAbcProject",
"primaryOutputs": [
{ "path": "NewAbcProject.csproj" }
],
"postActions": [
{
"actionId": "B17581D1-C5C9-4489-8F0A-004BE667B814",
"continueOnError": true,
"args": {
"referenceType": "project",
"reference": "../NewAbcProject/NewAbcProject.csproj",
"targetFiles": "../Testbed/Testbed.csproj"
}
},
{
"actionId": "E3E04F23-D7EC-4F3D-B747-1D1C994E0C16",
"continueOnError": true,
"args": {
"executable": "dotnet",
"args": "add ../Testbed/Testbed.csproj reference NewAbcProject/NewAbcProject.csproj"
}
},
{
"actionId": "E3E04F23-D7EC-4F3D-B747-1D1C994E0C16",
"args": {
"executable": "AddReference.cmd",
"workingDirectory": "."
}
}
]
}
What I've tried
1. AddProjectReference post action
- Does not modify the Testbed project
- Visual Studio doesn't report errors
2. A script post action calling dotnet add
- Doesn't appear to run inside Visual Studio
- Visual Studio doesn't report errors
- Works when I run the same command myself
3. A script post action calling a .cmd file
- Doesn't appear to run inside Visual Studio
- Visual Studio doesn't report errors
- The
.cmdworks when executed manually
All without success.
Note: Because I'm never quite sure what relative paths are relative to, the above have been tried with multiple combinations of relative paths, none of which were successful.
Is this even possible?
Is there any supported way (in a Visual Studio project template) to automatically add a project reference to an existing project in the same solution?
Or is Visual Studio simply not capable of doing this?