예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        /// <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);
            }
        }
예제 #5
0
 private ComposableCatalog GetCatalog()
 => ExportProviderCache.CreateAssemblyCatalog(Assemblies, ExportProviderCache.CreateResolver()).WithoutPartsOfTypes(ExcludedPartTypes).WithParts(Parts);