diff --git a/src/Xunit.Vsix/GlobalServiceProvider.cs b/src/Xunit.Vsix/GlobalServiceProvider.cs index 8510640..ddb2ef7 100644 --- a/src/Xunit.Vsix/GlobalServiceProvider.cs +++ b/src/Xunit.Vsix/GlobalServiceProvider.cs @@ -14,8 +14,8 @@ namespace Xunit; [EditorBrowsable(EditorBrowsableState.Never)] public static class GlobalServiceProvider { - static readonly IServiceProvider instance; - static readonly dynamic components; + static readonly System.IServiceProvider instance; + static readonly Lazy components; static GlobalServiceProvider() { @@ -30,8 +30,10 @@ static GlobalServiceProvider() else { instance = new OleServiceProvider(dte); - components = instance.GetService().AsDynamicReflection(); } + + components = new Lazy(() => + instance.GetService().AsDynamicReflection()); } catch (NotSupportedException) { @@ -64,12 +66,12 @@ static GlobalServiceProvider() /// /// Retrieves an exported MEF component from the currently running Visual Studio. /// - public static T GetExport() => components == null ? null : components?.GetService(); + public static T GetExport() => components == null ? null : components.Value?.GetService(); /// /// Retrieves exported MEF components from the currently running Visual Studio. /// - public static IEnumerable GetExports() => components?.GetExtensions() ?? Array.Empty(); + public static IEnumerable GetExports() => components.Value?.GetExtensions() ?? Array.Empty(); class NullServices : IServiceProvider { diff --git a/src/Xunit.Vsix/VsClient.cs b/src/Xunit.Vsix/VsClient.cs index 6cc6f6d..aac558f 100644 --- a/src/Xunit.Vsix/VsClient.cs +++ b/src/Xunit.Vsix/VsClient.cs @@ -320,7 +320,7 @@ bool Start() // Retrieve the component model service, which could also now take time depending on new // extensions being installed or updated before the first launch. - var components = services.GetService(); + //var components = services.GetService(); //if (Debugger.IsAttached) //{ diff --git a/src/Xunit.Vsix/VsRemoteRunner.cs b/src/Xunit.Vsix/VsRemoteRunner.cs index 7a62ec5..8bc8b8c 100644 --- a/src/Xunit.Vsix/VsRemoteRunner.cs +++ b/src/Xunit.Vsix/VsRemoteRunner.cs @@ -26,6 +26,7 @@ class VsRemoteRunner : MarshalByRefObject, IVsRemoteRunner { string _pipeName; IChannel _channel; + IServiceProvider _services; Dictionary _assemblyFixtureMappings = new Dictionary(); Dictionary _collectionFixtureMappings = new Dictionary(); @@ -55,6 +56,10 @@ public string[][] GetEnvironment() public VsixRunSummary Run(VsixTestCase testCase, IMessageBus messageBus) { + // Before the first test is run, ensure we have initialized the global services + // which in turn requests the component model which ensures MEF is initialized. + _services ??= GlobalServiceProvider.Default; + messageBus.QueueMessage(new DiagnosticMessage("Running {0}", testCase.DisplayName)); var aggregator = new ExceptionAggregator();