@@ -31,7 +31,7 @@ internal sealed partial class MiscellaneousFilesWorkspace : Workspace, IOpenText
31
31
private readonly IThreadingContext _threadingContext ;
32
32
private readonly IVsService < IVsTextManager > _textManagerService ;
33
33
private readonly OpenTextBufferProvider _openTextBufferProvider ;
34
- private readonly IMetadataAsSourceFileService _fileTrackingMetadataAsSourceService ;
34
+ private readonly Lazy < IMetadataAsSourceFileService > _fileTrackingMetadataAsSourceService ;
35
35
36
36
private readonly ConcurrentDictionary < Guid , LanguageInformation > _languageInformationByLanguageGuid = [ ] ;
37
37
@@ -47,24 +47,24 @@ internal sealed partial class MiscellaneousFilesWorkspace : Workspace, IOpenText
47
47
/// </summary>
48
48
private readonly Dictionary < string , ( ProjectId projectId , SourceTextContainer textContainer ) > _monikersToProjectIdAndContainer = [ ] ;
49
49
50
- private readonly ImmutableArray < MetadataReference > _metadataReferences ;
50
+ private readonly Lazy < ImmutableArray < MetadataReference > > _metadataReferences ;
51
51
52
52
[ ImportingConstructor ]
53
53
[ Obsolete ( MefConstruction . ImportingConstructorMessage , error : true ) ]
54
54
public MiscellaneousFilesWorkspace (
55
55
IThreadingContext threadingContext ,
56
56
IVsService < SVsTextManager , IVsTextManager > textManagerService ,
57
57
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 )
61
61
{
62
62
_threadingContext = threadingContext ;
63
63
_textManagerService = textManagerService ;
64
64
_openTextBufferProvider = openTextBufferProvider ;
65
65
_fileTrackingMetadataAsSourceService = fileTrackingMetadataAsSourceService ;
66
66
67
- _metadataReferences = [ .. CreateMetadataReferences ( ) ] ;
67
+ _metadataReferences = new ( ( ) => [ .. CreateMetadataReferences ( ) ] ) ;
68
68
69
69
_openTextBufferProvider . AddListener ( this ) ;
70
70
}
@@ -122,6 +122,10 @@ public void RegisterLanguage(Guid languageGuid, string languageName, string scri
122
122
123
123
private IEnumerable < MetadataReference > CreateMetadataReferences ( )
124
124
{
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
+
125
129
var manager = this . Services . GetService < VisualStudioMetadataReferenceManager > ( ) ;
126
130
var searchPaths = VisualStudioMetadataReferenceManager . GetReferencePaths ( ) ;
127
131
@@ -261,7 +265,7 @@ private void AttachToDocument(string moniker, ITextBuffer textBuffer)
261
265
{
262
266
_threadingContext . ThrowIfNotOnUIThread ( ) ;
263
267
264
- if ( _fileTrackingMetadataAsSourceService . TryAddDocumentToWorkspace ( moniker , textBuffer . AsTextContainer ( ) , out var _ ) )
268
+ if ( _fileTrackingMetadataAsSourceService . Value . TryAddDocumentToWorkspace ( moniker , textBuffer . AsTextContainer ( ) , out var _ ) )
265
269
{
266
270
// We already added it, so we will keep it excluded from the misc files workspace
267
271
return ;
@@ -282,20 +286,24 @@ private void AttachToDocument(string moniker, ITextBuffer textBuffer)
282
286
/// </summary>
283
287
private ProjectInfo CreateProjectInfoForDocument ( string filePath )
284
288
{
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
+
285
293
// This should always succeed since we only got here if we already confirmed the moniker is acceptable
286
294
var languageInformation = TryGetLanguageInformation ( filePath ) ;
287
295
Contract . ThrowIfNull ( languageInformation ) ;
288
296
289
297
var checksumAlgorithm = SourceHashAlgorithms . Default ;
290
298
var fileLoader = new WorkspaceFileTextLoader ( Services . SolutionServices , filePath , defaultEncoding : null ) ;
291
299
return MiscellaneousFileUtilities . CreateMiscellaneousProjectInfoForDocument (
292
- this , filePath , fileLoader , languageInformation , checksumAlgorithm , Services . SolutionServices , _metadataReferences ) ;
300
+ this , filePath , fileLoader , languageInformation , checksumAlgorithm , Services . SolutionServices , _metadataReferences . Value ) ;
293
301
}
294
302
295
303
private void DetachFromDocument ( string moniker )
296
304
{
297
305
_threadingContext . ThrowIfNotOnUIThread ( ) ;
298
- if ( _fileTrackingMetadataAsSourceService . TryRemoveDocumentFromWorkspace ( moniker ) )
306
+ if ( _fileTrackingMetadataAsSourceService . Value . TryRemoveDocumentFromWorkspace ( moniker ) )
299
307
{
300
308
return ;
301
309
}
0 commit comments