private IExportProviderFactory GetOrCreateFactory() { var key = new CacheKey(Assemblies, Parts, ExcludedPartTypes); lock (s_factoryCache) { if (s_factoryCache.TryGetValue(key, out var existing)) { return(existing); } } var newFactory = ExportProviderCache.CreateExportProviderFactory(GetCatalog(), Scope); lock (s_factoryCache) { if (s_factoryCache.TryGetValue(key, out var existing)) { return(existing); } s_factoryCache.Add(key, newFactory); } return(newFactory); }
private MefHostServices CreateMefHostServices(IEnumerable <Assembly> assemblies) { ExportProvider exportProvider; if (assemblies is ImmutableArray <Assembly> array && array == MefHostServices.DefaultAssemblies && ExportProviderCache.LocalExportProviderForCleanup != null) { if (_hostServices != null) { return(_hostServices); } exportProvider = ExportProviderCache.LocalExportProviderForCleanup; } else { exportProvider = ExportProviderCache.GetOrCreateExportProviderFactory(assemblies).CreateExportProvider(); } Interlocked.CompareExchange( ref _hostServices, new ExportProviderMefHostServices(exportProvider), null); return(_hostServices); }
public override void Before(MethodInfo?methodUnderTest) { #if false MefHostServices.TestAccessor.HookServiceCreation(CreateMefHostServices); // make sure we enable this for all unit tests AsynchronousOperationListenerProvider.Enable(enable: true, diagnostics: true); #endif ExportProviderCache.SetEnabled_OnlyUseExportProviderAttributeCanCall(true); }
/// <summary> /// To the extent reasonably possible, this method resets the state of the test environment to the same state as /// it started, ensuring that tests running in sequence cannot influence the outcome of later tests. /// </summary> /// <remarks> /// <para>The test cleanup runs in two primary steps:</para> /// <list type="number"> /// <item>Waiting for asynchronous operations started by the test to complete.</item> /// <item>Disposing of mutable resources created by the test.</item> /// <item>Clearing static state variables related to the use of MEF during a test.</item> /// </list> /// </remarks> public override void After(MethodInfo?methodUnderTest) { try { foreach (var exportProvider in ExportProviderCache.ExportProvidersForCleanup) { DisposeExportProvider(exportProvider); } } finally { #if false // Replace hooks with ones that always throw exceptions. These hooks detect cases where code executing // after the end of a test attempts to create an ExportProvider. MefHostServices.TestAccessor.HookServiceCreation(DenyMefHostServicesCreationBetweenTests); #endif // Reset static state variables. #if false _hostServices = null; #endif ExportProviderCache.SetEnabled_OnlyUseExportProviderAttributeCanCall(false); } }
private ComposableCatalog GetCatalog() => ExportProviderCache.CreateAssemblyCatalog(Assemblies, ExportProviderCache.CreateResolver()).WithoutPartsOfTypes(ExcludedPartTypes).WithParts(Parts);