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?