@@ -56,23 +56,12 @@ public partial class CompositionManager: Service
56
56
new AttributedPartDiscoveryV1 ( StandardResolver ) ,
57
57
new AttributedPartDiscovery ( StandardResolver , true ) ) ;
58
58
59
- static Action HandleMefQueriedBeforeCompletion = UninitializedLogWarning ;
60
-
61
- static void UninitializedLogWarning ( )
62
- => LoggingService . LogWarning ( "UI thread queried MEF while it was still being built:{0}{1}" , Environment . NewLine , Environment . StackTrace ) ;
63
-
64
- static void UninitializedThrowException ( )
65
- => throw new InvalidOperationException ( "MEF queried while it was still being built" ) ;
66
-
67
- internal static void ConfigureUninitializedMefHandling ( bool throwException )
68
- => HandleMefQueriedBeforeCompletion = throwException ? new Action ( UninitializedThrowException ) : new Action ( UninitializedLogWarning ) ;
69
-
70
59
public static CompositionManager Instance {
71
60
get {
72
61
if ( instance == null ) {
73
62
var task = Runtime . GetService < CompositionManager > ( ) ;
74
63
if ( ! task . IsCompleted && Runtime . IsMainThread ) {
75
- HandleMefQueriedBeforeCompletion ( ) ;
64
+ LoggingService . LogWarning ( "UI thread queried MEF while it was still being built:{0}{1}" , Environment . NewLine , Environment . StackTrace ) ;
76
65
}
77
66
instance = task . WaitAndGetResult ( ) ;
78
67
}
@@ -82,8 +71,20 @@ public static CompositionManager Instance {
82
71
}
83
72
84
73
protected override Task OnInitialize ( ServiceProvider serviceProvider )
85
- {
86
- return Task . Run ( async ( ) => await InitializeInstanceAsync ( ) ) ;
74
+ {
75
+ return Runtime . RunInMainThread ( ( ) => {
76
+ var timings = new Dictionary < string , long > ( ) ;
77
+ var metadata = new CompositionLoadMetadata ( timings ) ;
78
+
79
+ var timer = Counters . CompositionLoad . BeginTiming ( metadata ) ;
80
+ var stepTimer = System . Diagnostics . Stopwatch . StartNew ( ) ;
81
+
82
+ var mefAssemblies = ReadAssembliesFromAddins ( timer ) ;
83
+
84
+ timings [ "ReadFromAddins" ] = stepTimer . ElapsedMilliseconds ;
85
+
86
+ return Task . Run ( ( ) => InitializeInstanceAsync ( timer , mefAssemblies ) ) ;
87
+ } ) ;
87
88
}
88
89
89
90
/// <summary>
@@ -115,49 +116,42 @@ internal CompositionManager ()
115
116
{
116
117
}
117
118
118
- async Task InitializeInstanceAsync ( )
119
+ async Task InitializeInstanceAsync ( ITimeTracker < CompositionLoadMetadata > timer , HashSet < Assembly > mefAssemblies )
119
120
{
120
- var timings = new Dictionary < string , long > ( ) ;
121
- var metadata = new CompositionLoadMetadata ( timings ) ;
122
-
123
- using ( var timer = Counters . CompositionLoad . BeginTiming ( metadata ) ) {
124
- var fullTimer = System . Diagnostics . Stopwatch . StartNew ( ) ;
125
- var stepTimer = System . Diagnostics . Stopwatch . StartNew ( ) ;
126
-
127
- var mefAssemblies = ReadAssembliesFromAddins ( timer ) ;
128
- timings [ "ReadFromAddins" ] = stepTimer . ElapsedMilliseconds ;
129
- stepTimer . Restart ( ) ;
130
-
131
- var caching = new Caching ( mefAssemblies , new IdeRuntimeCompositionExceptionHandler ( ) ) ;
121
+ var metadata = timer . Metadata ;
122
+ var fullTimer = System . Diagnostics . Stopwatch . StartNew ( ) ;
123
+ var stepTimer = System . Diagnostics . Stopwatch . StartNew ( ) ;
132
124
133
- // Try to use cached MEF data
125
+ var caching = new Caching ( mefAssemblies , new IdeRuntimeCompositionExceptionHandler ( ) ) ;
134
126
127
+ // Try to use cached MEF data
128
+ using ( timer ) {
135
129
var canUse = metadata . ValidCache = caching . CanUse ( ) ;
136
- if ( canUse ) {
130
+ if ( canUse ) {
137
131
LoggingService . LogInfo ( "Creating MEF composition from cache" ) ;
138
132
RuntimeComposition = await TryCreateRuntimeCompositionFromCache ( caching ) ;
139
- }
140
- timings [ "LoadFromCache" ] = stepTimer . ElapsedMilliseconds ;
133
+ }
134
+ metadata . Timings [ "LoadFromCache" ] = stepTimer . ElapsedMilliseconds ;
141
135
stepTimer . Restart ( ) ;
142
136
143
137
// Otherwise fallback to runtime discovery.
144
- if ( RuntimeComposition == null ) {
138
+ if ( RuntimeComposition == null ) {
145
139
LoggingService . LogInfo ( "Creating MEF composition from runtime" ) ;
146
- var ( runtimeComposition , catalog ) = await CreateRuntimeCompositionFromDiscovery ( caching , timer ) ;
140
+ var ( runtimeComposition , catalog ) = await CreateRuntimeCompositionFromDiscovery ( caching , timer ) ;
147
141
RuntimeComposition = runtimeComposition ;
148
142
149
143
CachedComposition cacheManager = new CachedComposition ( ) ;
150
144
caching . Write ( RuntimeComposition , catalog , cacheManager ) . Ignore ( ) ;
151
- }
152
- timings [ "LoadRuntimeComposition" ] = stepTimer . ElapsedMilliseconds ;
153
- stepTimer . Restart ( ) ;
145
+ }
146
+ metadata . Timings [ "LoadRuntimeComposition" ] = stepTimer . ElapsedMilliseconds ;
147
+ stepTimer . Restart ( ) ;
154
148
155
149
ExportProviderFactory = RuntimeComposition . CreateExportProviderFactory ( ) ;
156
150
ExportProvider = ExportProviderFactory . CreateExportProvider ( ) ;
157
151
HostServices = Microsoft . VisualStudio . LanguageServices . VisualStudioMefHostServices . Create ( ExportProvider ) ;
158
-
159
- timings [ "CreateServices" ] = stepTimer . ElapsedMilliseconds ;
160
- metadata . Duration = fullTimer . ElapsedMilliseconds ;
152
+
153
+ metadata . Timings [ "CreateServices" ] = stepTimer . ElapsedMilliseconds ;
154
+ metadata . Duration = fullTimer . ElapsedMilliseconds ;
161
155
}
162
156
}
163
157
0 commit comments