提交 90d53c9c 编写于 作者: J Jason Malinowski

Move call to RegisterDocumentOptionsProvider to TestWorkspace constructor

This code as was written was a bit shaky. The Workspace constructor
would ask for the workspace option service, and this would cause us
(while creating the service) to call RegisterDocumentOptionsProvider.
If that results in one of the options providers also asking for options,
we recursively request the option service while still trying to
construct it.

The fix is just to move the registration to the workspace constructor
itself which removes the circularity and matches the pattern used
everywhere else.
上级 f42de27b
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
namespace Microsoft.CodeAnalysis.Remote
{
......@@ -11,19 +13,31 @@ namespace Microsoft.CodeAnalysis.Remote
/// </summary>
internal class TemporaryWorkspace : Workspace
{
public TemporaryWorkspace(Solution solution)
private TemporaryWorkspace()
: base(RoslynServices.HostServices, workspaceKind: WorkspaceKind.RemoteTemporaryWorkspace)
{
Options = Options.WithChangedOption(CacheOptions.RecoverableTreeLengthThreshold, 0);
this.SetCurrentSolution(solution);
var documentOptionsProviderFactories = ((IMefHostExportProvider)Services.HostServices).GetExports<IDocumentOptionsProviderFactory>();
foreach (var factory in documentOptionsProviderFactories)
{
var documentOptionsProvider = factory.Value.TryCreate(this);
if (documentOptionsProvider != null)
{
Services.GetRequiredService<IOptionService>().RegisterDocumentOptionsProvider(documentOptionsProvider);
}
}
}
public TemporaryWorkspace(SolutionInfo solutionInfo)
: base(RoslynServices.HostServices, workspaceKind: WorkspaceKind.RemoteTemporaryWorkspace)
public TemporaryWorkspace(Solution solution) : this()
{
Options = Options.WithChangedOption(CacheOptions.RecoverableTreeLengthThreshold, 0);
this.SetCurrentSolution(solution);
}
public TemporaryWorkspace(SolutionInfo solutionInfo) : this()
{
this.OnSolutionAdded(solutionInfo);
}
......
......@@ -16,35 +16,20 @@ namespace Microsoft.CodeAnalysis.Remote
internal class TemporaryWorkspaceOptionsServiceFactory : IWorkspaceServiceFactory
{
private readonly ImmutableArray<Lazy<IOptionProvider>> _providers;
private readonly ImmutableArray<IDocumentOptionsProviderFactory> _documentOptionsProviderFactories;
[ImportingConstructor]
public TemporaryWorkspaceOptionsServiceFactory(
[ImportMany] IEnumerable<Lazy<IOptionProvider>> optionProviders,
[ImportMany] IEnumerable<IDocumentOptionsProviderFactory> documentOptionsProviderFactories)
[ImportMany] IEnumerable<Lazy<IOptionProvider>> optionProviders)
{
_providers = optionProviders.ToImmutableArray();
_documentOptionsProviderFactories = documentOptionsProviderFactories.ToImmutableArray();
}
public IWorkspaceService CreateService(HostWorkspaceServices workspaceServices)
{
// give out new option service per workspace
var service = new OptionServiceFactory.OptionService(
return new OptionServiceFactory.OptionService(
new GlobalOptionService(_providers, SpecializedCollections.EmptyEnumerable<Lazy<IOptionPersister>>()),
workspaceServices);
foreach (var factory in _documentOptionsProviderFactories)
{
var documentOptionsProvider = factory.TryCreate(workspaceServices.Workspace);
if (documentOptionsProvider != null)
{
service.RegisterDocumentOptionsProvider(documentOptionsProvider);
}
}
return service;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册