Skip to content

Commit af13c77

Browse files
author
Zubin Ramlakhan
committed
Use MEF hostservices to create workspace to avoid race condition.
1 parent 7e3d09e commit af13c77

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

CodeConverter/Shared/SolutionFileTextEditor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ private static (string Find, string Replace, bool FirstOnly) GetProjectGuidRepla
8989
var projGuidMatch = projGuidRegex.Match(contents);
9090

9191
if (!projGuidMatch.Success) {
92-
throw new OperationCanceledException($"{nameof(guidPattern)} {guidPattern} doesn't match with sourceSlnFileContents {contents}");
92+
throw new OperationCanceledException($"{nameof(guidPattern)} {guidPattern} doesn't match with" +
93+
$" sourceSlnFileContents {contents}");
9394
}
9495

9596
var oldGuid = projGuidMatch.Groups[1].Value;

CodeConverter/Shared/ThreadSafeWorkspaceHelper.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,21 @@ namespace ICSharpCode.CodeConverter.Shared
1818
public static class ThreadSafeWorkspaceHelper
1919
{
2020
/// <summary>
21-
/// Empty solution in an adhoc workspace
21+
/// Create an empty adhoc workspace
2222
/// </summary>
23-
public static AsyncLazy<Solution> EmptyAdhocSolution { get; } = new AsyncLazy<Solution>(async () => {
23+
public static AsyncLazy<AdhocWorkspace> CreateAdhocWorkspace { get; } = new(async () =>
24+
{
2425
var hostServices = await CreateHostServicesAsync(MefHostServices.DefaultAssemblies);
25-
return new AdhocWorkspace(hostServices).CurrentSolution;
26+
return new AdhocWorkspace(hostServices);
27+
}, JoinableTaskFactorySingleton.Instance);
28+
29+
/// <summary>
30+
/// Empty solution in an adhoc workspace
31+
/// </summary>
32+
public static AsyncLazy<Solution> EmptyAdhocSolution { get; } = new(async () =>
33+
{
34+
var adhocWorkspace = await CreateAdhocWorkspace.GetValueAsync();
35+
return adhocWorkspace.CurrentSolution;
2636
}, JoinableTaskFactorySingleton.Instance);
2737

2838
/// <summary>

Tests/LanguageAgnostic/SolutionFileTextEditorTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@ private static string GetProjectProjectReference(IReadOnlyCollection<string> rel
516516

517517
private static Solution CreateTestSolution()
518518
{
519-
var ws = new AdhocWorkspace();
519+
var ws = Task.Run(() => ThreadSafeWorkspaceHelper.CreateAdhocWorkspace.GetValueAsync())
520+
.GetAwaiter().GetResult();
520521
var solutionId = SolutionId.CreateNewId(SlnName);
521522
var versionStamp = VersionStamp.Create();
522523
var solutionInfo = SolutionInfo.Create(solutionId, versionStamp, SlnFilePath);

0 commit comments

Comments
 (0)