3

What I am doing wrong here? The following code fails (and it's taken from Roslyn's source website)

public class CompletionServiceTests
{
    public void AcquireCompletionService()
    {
        var workspace = new AdhocWorkspace();

        var document = workspace
            .AddProject("TestProject", LanguageNames.CSharp)
            .AddDocument("TestDocument.cs", "");

        var service = CompletionService.GetService(document);
        Assert.NotNull(service);
    }
}
edeboursetty
  • 5,669
  • 2
  • 40
  • 67

2 Answers2

6

I found the problem. I needed to do this instead:

        var assemblies = new[]
        {
            Assembly.Load("Microsoft.CodeAnalysis"),
            Assembly.Load("Microsoft.CodeAnalysis.CSharp"),
            Assembly.Load("Microsoft.CodeAnalysis.Features"),
            Assembly.Load("Microsoft.CodeAnalysis.CSharp.Features"),
        };

        var partTypes = MefHostServices.DefaultAssemblies.Concat(assemblies)
                .Distinct()
                .SelectMany(x => x.GetTypes())
                .ToArray();

        var compositionContext = new ContainerConfiguration()
            .WithParts(partTypes)
            .CreateContainer();

        var host = MefHostServices.Create(compositionContext);

        var workspace = new AdhocWorkspace(host);

        var document = workspace
            .AddProject("TestProject", LanguageNames.CSharp)
            .AddDocument("TestDocument.cs", "");

        var service = CompletionService.GetService(document);
edeboursetty
  • 5,669
  • 2
  • 40
  • 67
  • for me, loading assemblies in this format `Assembly.Load("Microsoft.CodeAnalysis, Version=1.3.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")` is necessary – Augustas Jul 28 '20 at 13:04
0

Adding the Microsoft.CodeAnalysis.CSharp.Features NuGet package solved it for me.

anakic
  • 2,746
  • 1
  • 30
  • 32