问题
I feel like there should be an easy way to do this. I mean it can't be possible that you can't add projects via extensions?
What I want to do:
I want to write a small extension that adds a "code refactoring" option to methods that will generate a test class and method for it in the matching test project. If the test project does not exist, I will create it.
Everything is working so far, except that when I try to use Solution.AddProject()
it throws the exception System.NotSupportedException : Adding projects is not supported.
.
Feels like the active workspace does not allow adding projects to it. I've read something about Microsoft.CodeAnalysis.CustomWorkspace
but it does not work. And AdhocWorkspace
does not persist any changes, right?
My code (simplified) so far:
private static async Task<Solution> CreateTest(Document document, MethodDeclarationSyntax method, CancellationToken cancellationToken)
{
var solution = document.Project.Solution;
// Try to find existing test project
var projectName = document.Project.Name + ".UnitTests";
var testProject = solution.Projects.FirstOrDefault(x => x.Name == projectName);
if (testProject == null)
{
// Create the project and update the solution
testProject = solution.AddProject(projectName, projectName, LanguageNames.CSharp); // <----- THIS IS FAILING
solution = testProject.Solution;
}
// Try to find existing test document
var documentName = $"{((TypeDeclarationSyntax) method.Parent).Identifier.Text}Tests.cs";
var testDocument = testProject.Documents.FirstOrDefault(x => x.Name == documentName);
if (testDocument == null)
{
// Create the document and update the project and solution
testDocument = testProject.AddDocument(documentName, "", document.Folders);
testProject = testDocument.Project;
solution = testProject.Solution;
}
return solution;
}
The preview of Roslyn code changes does work correct and says it will add the correct project. When I click it, the following exception occurs
System.AggregateException : One or more errors occurred. ---> Adding projects is not supported.
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout,CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.InvokeWorker(Func`1 getFromDocument,IProgressTracker progressTracker,CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Editor.Implementation.Suggestions.SuggestedAction.<>c__DisplayClass18_0.<InvokeCore>b__0()
at Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformAction(IExtensionManager extensionManager,Object extension,Action action)
---> (Inner Exception #0) System.NotSupportedException : Adding projects is not supported.
at Microsoft.CodeAnalysis.Workspace.CheckAllowedSolutionChanges(SolutionChanges solutionChanges)
at Microsoft.CodeAnalysis.Workspace.TryApplyChanges(Solution newSolution,IProgressTracker progressTracker)
at Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem.VisualStudioWorkspaceImpl.TryApplyChanges(Solution newSolution,IProgressTracker progressTracker)
at Microsoft.CodeAnalysis.CodeActions.ApplyChangesOperation.TryApply(Workspace workspace,IProgressTracker progressTracker,CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Editor.Implementation.CodeActions.CodeActionEditHandlerService.ProcessOperations(Workspace workspace,ImmutableArray`1 operations,IProgressTracker progressTracker,CancellationToken cancellationToken)
at async Microsoft.CodeAnalysis.Editor.Implementation.CodeActions.CodeActionEditHandlerService.ApplyAsync(<Unknown Parameters>)<---
So I guess ApplyChangesKind.AddProject
is set to false
for the workspace.
What's the way to fix this? Or the correct way to add projects with Roslyn?
For all who want to test it the quick way I have included the full code: https://gist.github.com/Wolfsblvt/1420c4ddd11f9386cb3ab2dadff313cb
来源:https://stackoverflow.com/questions/44020972/vsix-adding-projects-with-roslyn-refactoring-options-says-not-supported