3

I am trying to get a reference to currently loaded workspace, without success. As per documentation (part in bold) I should be able to get a reference to it.

The Workspace APIs are found in the Roslyn.Services namespace, and they are available if you include the following using directive:

using Roslyn.Services;

The workspace you use will typically be provided directly by the host environment (such as the Visual Studio IDE). However, you can work with a workspace outside of a host environment by constructing your own IWorkspace instance.

You can construct a workspace by loading a solution file.

IWorkspace workspace = Workspace.LoadSolution(@"HelloWorld.sln"); ISolution solution = workspace.CurrentSolution;

I tried following in unit test but workspace is null.

  IWorkspace workspace = Workspace.PrimaryWorkspace;

  ISolution solution = workspace.CurrentSolution;

I dont want to load solution, I want to work within currently loaded solution. How is it done? I am using Visual Studio 2012.

Edit:

Tried using switch /rootSuffx Roslyn as suggested in answer and VS throws an error that it is invalid switch. Changed it to /rootSuffix Roslyn, and VS starts but workspace is still null.

epitka
  • 17,275
  • 20
  • 88
  • 141
  • 1
    Have you seen this answer http://social.msdn.microsoft.com/Forums/vstudio/en-US/d37e72dc-d04e-4a60-860f-7487c94b3620/how-to-work-with-workspaceprimaryworkspacecurrentsolution-inside-vspackage?forum=roslyn? – nemesv Oct 24 '13 at 20:34
  • @nemesv: No, I did not, thank you – epitka Oct 24 '13 at 20:39

1 Answers1

2

As described by Dustin Campbell in his answer here: How to work with Workspace.PrimaryWorkspace.CurrentSolution inside VSPackage

The primary workspace inside of Visual Studio is only populated when the Roslyn C# and Visual Basic language services are enabled.

To enable the Roslyn languages services you need to start your VS with the following command:

devenv.exe /rootSuffix Roslyn
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • Yeah I had typo but you need the `Roslyn` part also so the full command is `"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe" /rootSuffix Roslyn` – nemesv Oct 24 '13 at 21:00
  • By the way which unit testing framework and test runner are you using? Are you using the special Roslyn project types? – nemesv Oct 24 '13 at 21:02
  • NUnit, and yes I have Roslyn at the end. – epitka Oct 25 '13 at 12:10
  • @epitka the problem is that the `Workspace.PrimaryWorkspace` get set by the hosting environment. And Visual Studio only sets this when started with `devenv.exe /rootSuffix Roslyn` AND your code is running inside one of the special roslyn porjects like: Code Issue, Code refactoring. So with the CTP the `Workspace.PrimaryWorkspace` will be always null in your unit test project... – nemesv Oct 25 '13 at 13:58