@@ -31,7 +31,7 @@ internal sealed partial class MiscellaneousFilesWorkspace : Workspace, IOpenText
3131 private readonly IThreadingContext _threadingContext ;
3232 private readonly IVsService < IVsTextManager > _textManagerService ;
3333 private readonly OpenTextBufferProvider _openTextBufferProvider ;
34- private readonly IMetadataAsSourceFileService _fileTrackingMetadataAsSourceService ;
34+ private readonly Lazy < IMetadataAsSourceFileService > _fileTrackingMetadataAsSourceService ;
3535
3636 private readonly ConcurrentDictionary < Guid , LanguageInformation > _languageInformationByLanguageGuid = [ ] ;
3737
@@ -47,24 +47,24 @@ internal sealed partial class MiscellaneousFilesWorkspace : Workspace, IOpenText
4747 /// </summary>
4848 private readonly Dictionary < string , ( ProjectId projectId , SourceTextContainer textContainer ) > _monikersToProjectIdAndContainer = [ ] ;
4949
50- private readonly ImmutableArray < MetadataReference > _metadataReferences ;
50+ private readonly Lazy < ImmutableArray < MetadataReference > > _metadataReferences ;
5151
5252 [ ImportingConstructor ]
5353 [ Obsolete ( MefConstruction . ImportingConstructorMessage , error : true ) ]
5454 public MiscellaneousFilesWorkspace (
5555 IThreadingContext threadingContext ,
5656 IVsService < SVsTextManager , IVsTextManager > textManagerService ,
5757 OpenTextBufferProvider openTextBufferProvider ,
58- IMetadataAsSourceFileService fileTrackingMetadataAsSourceService ,
59- VisualStudioWorkspace visualStudioWorkspace )
60- : base ( visualStudioWorkspace . Services . HostServices , WorkspaceKind . MiscellaneousFiles )
58+ Lazy < IMetadataAsSourceFileService > fileTrackingMetadataAsSourceService ,
59+ Composition . ExportProvider exportProvider )
60+ : base ( VisualStudioMefHostServices . Create ( exportProvider ) , WorkspaceKind . MiscellaneousFiles )
6161 {
6262 _threadingContext = threadingContext ;
6363 _textManagerService = textManagerService ;
6464 _openTextBufferProvider = openTextBufferProvider ;
6565 _fileTrackingMetadataAsSourceService = fileTrackingMetadataAsSourceService ;
6666
67- _metadataReferences = [ .. CreateMetadataReferences ( ) ] ;
67+ _metadataReferences = new ( ( ) => [ .. CreateMetadataReferences ( ) ] ) ;
6868
6969 _openTextBufferProvider . AddListener ( this ) ;
7070 }
@@ -122,6 +122,10 @@ public void RegisterLanguage(Guid languageGuid, string languageName, string scri
122122
123123 private IEnumerable < MetadataReference > CreateMetadataReferences ( )
124124 {
125+ // VisualStudioMetadataReferenceManager construction requires the main thread
126+ // TODO: Determine if main thread affinity can be removed: https://github.com/dotnet/roslyn/issues/77791
127+ _threadingContext . ThrowIfNotOnUIThread ( ) ;
128+
125129 var manager = this . Services . GetService < VisualStudioMetadataReferenceManager > ( ) ;
126130 var searchPaths = VisualStudioMetadataReferenceManager . GetReferencePaths ( ) ;
127131
@@ -261,7 +265,7 @@ private void AttachToDocument(string moniker, ITextBuffer textBuffer)
261265 {
262266 _threadingContext . ThrowIfNotOnUIThread ( ) ;
263267
264- if ( _fileTrackingMetadataAsSourceService . TryAddDocumentToWorkspace ( moniker , textBuffer . AsTextContainer ( ) , out var _ ) )
268+ if ( _fileTrackingMetadataAsSourceService . Value . TryAddDocumentToWorkspace ( moniker , textBuffer . AsTextContainer ( ) , out var _ ) )
265269 {
266270 // We already added it, so we will keep it excluded from the misc files workspace
267271 return ;
@@ -282,20 +286,24 @@ private void AttachToDocument(string moniker, ITextBuffer textBuffer)
282286 /// </summary>
283287 private ProjectInfo CreateProjectInfoForDocument ( string filePath )
284288 {
289+ // Potential calculation of _metadataReferences requires being on the main thread
290+ // TODO: Determine if main thread affinity can be removed: https://github.com/dotnet/roslyn/issues/77791
291+ _threadingContext . ThrowIfNotOnUIThread ( ) ;
292+
285293 // This should always succeed since we only got here if we already confirmed the moniker is acceptable
286294 var languageInformation = TryGetLanguageInformation ( filePath ) ;
287295 Contract . ThrowIfNull ( languageInformation ) ;
288296
289297 var checksumAlgorithm = SourceHashAlgorithms . Default ;
290298 var fileLoader = new WorkspaceFileTextLoader ( Services . SolutionServices , filePath , defaultEncoding : null ) ;
291299 return MiscellaneousFileUtilities . CreateMiscellaneousProjectInfoForDocument (
292- this , filePath , fileLoader , languageInformation , checksumAlgorithm , Services . SolutionServices , _metadataReferences ) ;
300+ this , filePath , fileLoader , languageInformation , checksumAlgorithm , Services . SolutionServices , _metadataReferences . Value ) ;
293301 }
294302
295303 private void DetachFromDocument ( string moniker )
296304 {
297305 _threadingContext . ThrowIfNotOnUIThread ( ) ;
298- if ( _fileTrackingMetadataAsSourceService . TryRemoveDocumentFromWorkspace ( moniker ) )
306+ if ( _fileTrackingMetadataAsSourceService . Value . TryRemoveDocumentFromWorkspace ( moniker ) )
299307 {
300308 return ;
301309 }
0 commit comments