diff --git a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/FallbackProjectManager.cs b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/FallbackProjectManager.cs index 9d48c5eaaca..a237a3c0511 100644 --- a/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/FallbackProjectManager.cs +++ b/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/ProjectSystem/FallbackProjectManager.cs @@ -114,11 +114,18 @@ private async Task AddFallbackProjectAsync(ProjectId projectId, string filePath, // the project will be updated, and it will no longer be a fallback project. var hostProject = new FallbackHostProject(project.FilePath, intermediateOutputPath, FallbackRazorConfiguration.Latest, rootNamespace, project.Name); - await _dispatcher - .RunOnDispatcherThreadAsync( - () => _projectManagerAccessor.Instance.ProjectAdded(hostProject), - cancellationToken) - .ConfigureAwait(false); + if (_dispatcher.IsDispatcherThread) + { + _projectManagerAccessor.Instance.ProjectAdded(hostProject); + } + else + { + await _dispatcher + .RunOnDispatcherThreadAsync( + () => _projectManagerAccessor.Instance.ProjectAdded(hostProject), + cancellationToken) + .ConfigureAwait(false); + } await AddFallbackDocumentAsync(hostProject.Key, filePath, project.FilePath, cancellationToken).ConfigureAwait(false); @@ -137,11 +144,18 @@ private async Task AddFallbackDocumentAsync(ProjectKey projectKey, string filePa var textLoader = new FileTextLoader(filePath, defaultEncoding: null); - await _dispatcher - .RunOnDispatcherThreadAsync( - () => _projectManagerAccessor.Instance.DocumentAdded(projectKey, hostDocument, textLoader), - cancellationToken) - .ConfigureAwait(false); + if (_dispatcher.IsDispatcherThread) + { + _projectManagerAccessor.Instance.DocumentAdded(projectKey, hostDocument, textLoader); + } + else + { + await _dispatcher + .RunOnDispatcherThreadAsync( + () => _projectManagerAccessor.Instance.DocumentAdded(projectKey, hostDocument, textLoader), + cancellationToken) + .ConfigureAwait(false); + } } private static HostDocument? CreateHostDocument(string filePath, string projectFilePath) @@ -180,11 +194,18 @@ private async Task RemoveFallbackDocumentAsync(ProjectId projectId, string fileP return; } - await _dispatcher - .RunOnDispatcherThreadAsync( - () => _projectManagerAccessor.Instance.DocumentRemoved(razorProjectKey, hostDocument), - cancellationToken) - .ConfigureAwait(false); + if (_dispatcher.IsDispatcherThread) + { + _projectManagerAccessor.Instance.DocumentRemoved(razorProjectKey, hostDocument); + } + else + { + await _dispatcher + .RunOnDispatcherThreadAsync( + () => _projectManagerAccessor.Instance.DocumentRemoved(razorProjectKey, hostDocument), + cancellationToken) + .ConfigureAwait(false); + } } private Project? TryFindProjectForProjectId(ProjectId projectId)