Add new project to solution as part of refactoring

Viewed 319

Is there a way to add a new project to solution from CodeAction class?

Context:

I have my CodeRefactoringProvider that generates some code and I want to place that code in a new project.

I have CodeRefactoringProvider and CodeAction implemented for that purpose and trying to add project from Solution instance that I'm getting from CodeRefactoringContext like

 context.RegisterRefactoring(
            CodeAction.Create("Create New Project",
            (c)=>                
                Task.Run( 
                    ()=>
                    {
                        var proj = context.Document.Project.Solution.AddProject("NewProject", "NewProject", "C#");
                        return proj.Solution;
                    })           
            ));

This gives me

    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>)<---

I tried to add project using IVsSolution service but got error:

 The operation could not be completed. A null reference pointer was passed to the stub. 

So, how can I create a new project in solution?

1 Answers

Actually you need to manipulate the VS UI, not solution file or its representation ( context.Document ). Otherwise, I would expect, that you will need to manipulate all well known "save changes first" functionality manually. What is complex.

Therefore you should call VS UI methods (means COM): Solution2.AddFromFile(String, Boolean)

https://docs.microsoft.com/en-us/dotnet/api/envdte80.solution2.addfromfile?view=visualstudiosdk-2017#EnvDTE80_Solution2_AddFromFile_System_String_System_Boolean_

This call would work as macro and it seems that what you are expected.

Related