none
Why can't Roslyn.Services be used in a roslyn console application project

    Dotaz

  • I am trying to use the namespace Roslyn.Services in a roslyn console application (I know that this works with a roslyn refactoring project). The reason: I want to load a solution with all source files and references and then build up a Compilation. Then I want to do some syntax rewriting (using the SemanticModel). The whole project doesn't need to be a VS extension.

    Will I have to parse the .sln and .csproj files myself to build up a Compilation for an existing solution?

    25. ledna 2012 11:33

Odpovědi

  • Hi again. Thanks for your answers. I will answer my own question because I finally solved the problem.

    I should have added the actual reason for my question: I tried to do exactely what you describe in your answer: I start VS (with or without roslyn language doesn't matter), create a new roslyn console application project. Then I add a reference to C:\Program Files\Reference Assemblies\Microsoft\Roslyn\Roslyn.Services.dll. Afterwards I include the directive using Roslyn.Services; and try to compile.

    Then I get a compiler error and a compiler warning (I am using a german version of VS2010 and will try to translate the output - is it possible to get the english compiler output without reinstalling VS2010?)  :

    error: the type or namespace "Services" is not part of the namespace "Roslyn" (missing assembly reference?)

    warining: the assembly "Roslyn.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" that is referenced can't be resolved because of a dependency on "Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" (not present in the current traget framework ".NETFramework,Version=v4.0,Profile=Client"). Delete the references to assemblys that are not present in the target framework or reassign the project.

    I just stumbled over the solution of my problem (https://connect.microsoft.com/VisualStudio/feedback/details/510573/referenced-assembly-could-not-be-resolved-because-it-has-a-dependency-conflict):

    The problem is "Profile=Client". I had to change the target framework...
    27. ledna 2012 10:13

Všechny reakce

  • Hi Jochen - You should be able to accomplish this using the Workspace API. Just create a Roslyn Console Application (File -> New Project -> Visual C# / Basic -> Roslyn -> Console Application) and add reference to Roslyn.Services.dll. You can then write some code like below to load a solution into a Workspace and work with the Compilations and SemanticModels for your projects and code files.

    using Roslyn.Services;

    namespace RoslynConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                var workspace = Workspace.LoadSolution("<Path to solution file>");
                foreach (var project in workspace.CurrentSolution.Projects)
                {
                    var compilation = project.GetCompilation();
                    foreach (var document in project.Documents)
                    {
                        var semanticModel = document.GetSemanticModel();
                        // ...
                    }
                }
            }
        }
    }

    There is one known issue related to loading workspaces under different platforms / configurations - you can find a description of the issue and a workaround on this forum post.

    Hope this helps!


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
    25. ledna 2012 20:09
    Vlastník
  • The Roslyn Project Overview document gives a brief overview about how the Roslyn APIs are layered (i.e. which APIs need VS and which don't) - (see section 2). This document is also copied to your machine when you install the CTP.

    In general, it should be possible to consume Roslyn's Compiler APIs, Scripting APIs and Workspace APIs from within standalone applications (i.e. these APIs shouldn't have any dependency on VS). However, you would need to be inside a VS extension to consume the Roslyn Editor Services APIs (such as formatting, intellisense etc.). As Dustin mentions here, we are also working on pushing some traditionally VS services (like IFindReferencesService) down to a layer where you wouldn't need VS.

    The names of the namespaces / dlls could probably do a better job of identifing the underlying APIs more clearly. For example, it is not immediately clear / discoverable that the Workspace API is available in the Roslyn.Services namespace within Roslyn.Services.dll. We are working on improving this naming / factoring. Below is a brief overview of where different APIs live in the CTP and whether / not they need VS.

    Note: This is subject to change in the future.
    APIs DLLs Namespaces Need VS?
    Compiler APIs Roslyn.Compilers.dll
    Roslyn.Compilers.CSharp.dll
    Roslyn.Compilers.VisualBasic.dll
    Roslyn.Compilers
    Roslyn.Compilers.Common
    Roslyn.Compilers.CSharp
    Roslyn.Compilers.VisualBasic
    No
    Scripting APIs Roslyn.Compilers.dll
    Roslyn.Compilers.CSharp.dll
    Roslyn.Scripting
    Roslyn.Scripting.CSharp
    No
    Workspace APIs Roslyn.Services.dll Roslyn.Services No
    Editor Services APIs Roslyn.Services.Editor.dll Roslyn.Services.Editor Yes
    25. ledna 2012 20:44
    Vlastník
  • Hi again. Thanks for your answers. I will answer my own question because I finally solved the problem.

    I should have added the actual reason for my question: I tried to do exactely what you describe in your answer: I start VS (with or without roslyn language doesn't matter), create a new roslyn console application project. Then I add a reference to C:\Program Files\Reference Assemblies\Microsoft\Roslyn\Roslyn.Services.dll. Afterwards I include the directive using Roslyn.Services; and try to compile.

    Then I get a compiler error and a compiler warning (I am using a german version of VS2010 and will try to translate the output - is it possible to get the english compiler output without reinstalling VS2010?)  :

    error: the type or namespace "Services" is not part of the namespace "Roslyn" (missing assembly reference?)

    warining: the assembly "Roslyn.Services, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" that is referenced can't be resolved because of a dependency on "Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" (not present in the current traget framework ".NETFramework,Version=v4.0,Profile=Client"). Delete the references to assemblys that are not present in the target framework or reassign the project.

    I just stumbled over the solution of my problem (https://connect.microsoft.com/VisualStudio/feedback/details/510573/referenced-assembly-could-not-be-resolved-because-it-has-a-dependency-conflict):

    The problem is "Profile=Client". I had to change the target framework...
    27. ledna 2012 10:13
  • Aha - I see. Thanks for clarifying. Normally you shouldn't have to browse to the assembly on disk - Roslyn.Services.dll should show up in the 'Add Reference' dialog under the '.NET' tab (because the CTP installer GACs all Roslyn.* assemblies). But because console applications (incl. Roslyn console applications) target the 'Client' profile by default (i.e. Project Properties->Application Tab->Target framework=".Net Framework 4 Client Profile"), the 'Add Reference' dialog is filtered to include only those references that target the 'Client' profile.

    If you change your console application project to target the 'Full' profile (i.e. Project Properties->Application Tab->Target framework=".Net Framework 4") then the filtering of the 'Add Reference' dialog will change to also include assemblies that target the 'Full' profile. Roslyn.Services.dll should show up in the list and you should now be able to add it from there.


    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
    27. ledna 2012 20:05
    Vlastník
  • Oh - thats good to know. I was wondering why the Roslyn.Services Namespace didn't appear in the .NET tab. I didn't know that there a is a "full" and a "client" .NET framework.
    28. ledna 2012 16:51
  • Yes .NET Framework comes in two flavors 'client profile' and 'full profile' :) I think the client profile was introduced in NetFX 3.5 SP1. You can read more about this here on MSDN.
    Shyam Namboodiripad | Software Development Engineer in Test | Roslyn Compilers Team
    30. ledna 2012 23:18
    Vlastník