Example #1
0
        public static ComposableCatalog CreateAssemblyCatalog(IEnumerable <Assembly> assemblies)
        {
            // If we run CreatePartsAsync on the test thread we may deadlock since it'll schedule stuff back
            // on the thread.
            var parts = Task.Run(async() => await s_partDiscovery.CreatePartsAsync(assemblies).ConfigureAwait(false)).Result;

            return(ComposableCatalog.Create(Resolver.DefaultInstance).AddParts(parts));
        }
 private static Task <DiscoveredParts> RegisterMultipleAsync()
 {
     return(partDiscovery.CreatePartsAsync(
                typeof(SimpleAdapterOne),
                typeof(SimpleAdapterTwo),
                typeof(SimpleAdapterThree),
                typeof(SimpleAdapterFour),
                typeof(SimpleAdapterFive),
                typeof(ImportMultiple1),
                typeof(ImportMultiple2),
                typeof(ImportMultiple3)));
 }
Example #3
0
        internal static async Task <IContainer> CreateContainerV3Async(IReadOnlyList <Assembly> assemblies, CompositionEngines attributesDiscovery, ITestOutputHelper output, Type[] parts = null)
        {
            PartDiscovery discovery     = GetDiscoveryService(attributesDiscovery);
            var           assemblyParts = await discovery.CreatePartsAsync(assemblies);

            var catalog = EmptyCatalog.AddParts(assemblyParts);

            if (parts != null && parts.Length != 0)
            {
                var typeCatalog = EmptyCatalog.AddParts(await discovery.CreatePartsAsync(parts));
                catalog = EmptyCatalog.AddParts(catalog.Parts.Concat(typeCatalog.Parts));
            }

            return(await CreateContainerV3Async(catalog, attributesDiscovery, output));
        }
Example #4
0
        public async Task GetAssemblyInputs_SucceedesWithGenericType(PartDiscovery discovery)
        {
            var catalog = TestUtilities.EmptyCatalog.AddParts(
                await discovery.CreatePartsAsync(typeof(ExportingWithGenerics <string>)));

            catalog.GetInputAssemblies();
        }
Example #5
0
        async Task InitializeInstanceAsync()
        {
            ComposableCatalog catalog = ComposableCatalog.Create(StandardResolver)
                                        .WithCompositionService()
                                        .WithDesktopSupport();

            var assemblies = new HashSet <Assembly> ();

            ReadAssembliesFromAddins(assemblies, "/MonoDevelop/Ide/TypeService/PlatformMefHostServices");
            ReadAssembliesFromAddins(assemblies, "/MonoDevelop/Ide/TypeService/MefHostServices");
            ReadAssembliesFromAddins(assemblies, "/MonoDevelop/Ide/Composition");

            // spawn discovery tasks in parallel for each assembly
            var tasks = new List <Task <DiscoveredParts> > (assemblies.Count);

            foreach (var assembly in assemblies)
            {
                var task = Task.Run(() => Discovery.CreatePartsAsync(assembly));
                tasks.Add(task);
            }

            foreach (var task in tasks)
            {
                catalog = catalog.AddParts(await task);
            }

            var discoveryErrors = catalog.DiscoveredParts.DiscoveryErrors;

            if (!discoveryErrors.IsEmpty)
            {
                foreach (var error in discoveryErrors)
                {
                    LoggingService.LogInfo("MEF discovery error", error);
                }

                // throw new ApplicationException ("MEF discovery errors");
            }

            CompositionConfiguration configuration = CompositionConfiguration.Create(catalog);

            if (!configuration.CompositionErrors.IsEmpty)
            {
                // capture the errors in an array for easier debugging
                var errors = configuration.CompositionErrors.SelectMany(e => e).ToArray();
                foreach (var error in errors)
                {
                    LoggingService.LogInfo("MEF composition error: " + error.Message);
                }

                // For now while we're still transitioning to VSMEF it's useful to work
                // even if the composition has some errors. TODO: re-enable this.
                //configuration.ThrowOnErrors ();
            }

            RuntimeComposition    = RuntimeComposition.CreateRuntimeComposition(configuration);
            ExportProviderFactory = RuntimeComposition.CreateExportProviderFactory();
            ExportProvider        = ExportProviderFactory.CreateExportProvider();
            HostServices          = MefV1HostServices.Create(ExportProvider.AsExportProvider());
            ExportProviderV1      = NetFxAdapters.AsExportProvider(ExportProvider);
        }
Example #6
0
        public async Task CreateFromTypesOmitsNonParts(PartDiscovery discovery)
        {
            var catalog = ComposableCatalog.Create(discovery.Resolver).AddParts(
                await discovery.CreatePartsAsync(typeof(NonExportingType), typeof(ExportingType)));

            Assert.Equal(1, catalog.Parts.Count);
            Assert.Equal(typeof(ExportingType), catalog.Parts.Single().Type);
        }
Example #7
0
        public async Task GetAssemblyInputs_WithImports(PartDiscovery discovery)
        {
            var catalog = TestUtilities.EmptyCatalog.AddParts(
                await discovery.CreatePartsAsync(
                    typeof(ExportingType),
                    typeof(ImportingType)));

            catalog.GetInputAssemblies();
        }
Example #8
0
        internal static async Task <CompositionConfiguration> CreateConfigurationAsync(CompositionEngines attributesDiscovery, params Type[] parts)
        {
            PartDiscovery discovery     = GetDiscoveryService(attributesDiscovery);
            var           assemblyParts = await discovery.CreatePartsAsync(parts);

            var catalog = EmptyCatalog.AddParts(assemblyParts);

            return(CompositionConfiguration.Create(catalog));
        }
Example #9
0
        public async Task GetAssemblyInputs_ContainsDefiningAttributeAssemblyForMetadata(PartDiscovery discovery)
        {
            var catalog = TestUtilities.EmptyCatalog.AddParts(
                await discovery.CreatePartsAsync(typeof(ExportingTypeWithMetadataWhoseDefiningAttributeIsInAnotherAssembly)));

            var inputAssemblies = catalog.GetInputAssemblies();

            Assert.Contains(typeof(SomeMetadataAttributeFromAnotherAssemblyAttribute).GetTypeInfo().Assembly.GetName(), inputAssemblies, AssemblyNameComparer.Default);
        }
Example #10
0
        public async Task DiscoveriesCombinedWithResolver()
        {
            LoggingAssemblyLoader assemblyLoader = new();
            Resolver      assemblyResolver       = new(assemblyLoader);
            PartDiscovery combined = PartDiscovery.Combine(assemblyResolver, TestUtilities.V2Discovery, TestUtilities.V1Discovery);

            Assert.Same(assemblyResolver, combined.Resolver);
            string testAssemblyPath = typeof(PartDiscoveryTests).Assembly.Location;
            await combined.CreatePartsAsync(new string[] { testAssemblyPath });

            Assert.Equal(new[] { testAssemblyPath }, assemblyLoader.AttemptedAssemblyPaths);
        }
Example #11
0
        public async Task GetAssemblyInputs_IdentifiesAssembliesDefiningParts(PartDiscovery discovery)
        {
            var catalog = TestUtilities.EmptyCatalog.AddParts(
                await discovery.CreatePartsAsync(typeof(NonExportingType), typeof(ExportingType)));

            var expected = new HashSet <AssemblyName>(AssemblyNameComparer.Default)
            {
                typeof(NonExportingType).GetTypeInfo().Assembly.GetName(),
                typeof(object).GetTypeInfo().Assembly.GetName(),
            };
            var actual = catalog.GetInputAssemblies();

            this.AssertExpectedInputAssemblies(expected, actual);
        }
Example #12
0
        public async Task GetAssemblyInputs_FunctionsCorrectlyWithNullMetadata(PartDiscovery discovery)
        {
            var catalog = TestUtilities.EmptyCatalog.AddParts(
                await discovery.CreatePartsAsync(typeof(ExportingTypeWithNullExportMetadata)));

            var expected = new HashSet <AssemblyName>(AssemblyNameComparer.Default)
            {
                typeof(AssemblyDiscoveryTests.ISomeInterface).GetTypeInfo().Assembly.GetName(),
                typeof(ExportingTypeWithNullExportMetadata).GetTypeInfo().Assembly.GetName(),
                typeof(object).GetTypeInfo().Assembly.GetName()
            };

            var actual = catalog.GetInputAssemblies();

            this.AssertExpectedInputAssemblies(expected, actual);
        }
Example #13
0
        public async Task GetAssemblyInputs_IdentifiesAssembliesDefiningExportingMembersWithTypeMetadata(PartDiscovery discovery)
        {
            var catalog = TestUtilities.EmptyCatalog.AddParts(
                await discovery.CreatePartsAsync(typeof(ExportingWithExportingMembers)));

            var expected = new HashSet <AssemblyName>(AssemblyNameComparer.Default)
            {
                typeof(AssemblyDiscoveryTests.ISomeInterface).GetTypeInfo().Assembly.GetName(),
                typeof(ExportingWithExportingMembers).GetTypeInfo().Assembly.GetName(),
                typeof(object).GetTypeInfo().Assembly.GetName()
            };

            var actual = catalog.GetInputAssemblies();

            this.AssertExpectedInputAssemblies(expected, actual);
        }
Example #14
0
        public static async Task <ComposableCatalog> CreateCatalog(HashSet <Assembly> assemblies = null)
        {
            Resolver      StandardResolver = Resolver.DefaultInstance;
            PartDiscovery Discovery        = PartDiscovery.Combine(
                new AttributedPartDiscoveryV1(StandardResolver),
                new AttributedPartDiscovery(StandardResolver, true));

            var parts = await Discovery.CreatePartsAsync(assemblies ?? ReadAssembliesFromAddins());

            ComposableCatalog catalog = ComposableCatalog.Create(StandardResolver)
                                        .WithCompositionService()
                                        .WithDesktopSupport()
                                        .AddParts(parts);

            return(catalog);
        }
Example #15
0
        public async Task GetAssemblyInputs_IdentifiesAssembliesDefiningMultipleDifferentEnumMetadata(PartDiscovery discovery)
        {
            var catalog = TestUtilities.EmptyCatalog.AddParts(
                await discovery.CreatePartsAsync(typeof(ExportingWithMultipleDifferentEnumMetadata)));

            var expected = new HashSet <AssemblyName>(AssemblyNameComparer.Default)
            {
                typeof(AssemblyDiscoveryTests.SomeEnum).GetTypeInfo().Assembly.GetName(),
                typeof(AssemblyDiscoveryTests2.SomeOtherEnum).GetTypeInfo().Assembly.GetName(),
                typeof(ExportingWithMultipleDifferentEnumMetadata).GetTypeInfo().Assembly.GetName(),
                typeof(object).GetTypeInfo().Assembly.GetName()
            };

            var actual = catalog.GetInputAssemblies();

            this.AssertExpectedInputAssemblies(expected, actual);
        }
Example #16
0
        public async Task GetAssemblyInputs_RecursesThroughInterfaceTreeInMetadata(PartDiscovery discovery)
        {
            var catalog = TestUtilities.EmptyCatalog.AddParts(
                await discovery.CreatePartsAsync(typeof(ExportingTypeWithExportMetadataWithExternalDependenciesAndInterfaceTree)));

            var expected = new HashSet <AssemblyName>(AssemblyNameComparer.Default)
            {
                typeof(AssemblyDiscoveryTests.ISomeInterfaceWithBaseInterface).GetTypeInfo().Assembly.GetName(),
                typeof(AssemblyDiscoveryTests2.IBlankInterface).GetTypeInfo().Assembly.GetName(),
                typeof(ExportingTypeWithExportMetadataWithExternalDependenciesAndInterfaceTree).GetTypeInfo().Assembly.GetName(),
                typeof(object).GetTypeInfo().Assembly.GetName()
            };

            var actual = catalog.GetInputAssemblies();

            this.AssertExpectedInputAssemblies(expected, actual);
        }
Example #17
0
        internal static async Task <RuntimeComposition> CreateRuntimeCompositionFromDiscovery(Caching caching)
        {
            using (var timer = Counters.CompositionDiscovery.BeginTiming()) {
                var parts = await Discovery.CreatePartsAsync(caching.Assemblies);

                timer.Trace("Composition parts discovered");

                ComposableCatalog catalog = ComposableCatalog.Create(StandardResolver)
                                            .WithCompositionService()
                                            .WithDesktopSupport()
                                            .AddParts(parts);

                var discoveryErrors = catalog.DiscoveredParts.DiscoveryErrors;
                if (!discoveryErrors.IsEmpty)
                {
                    foreach (var error in discoveryErrors)
                    {
                        LoggingService.LogInfo("MEF discovery error", error);
                    }

                    // throw new ApplicationException ("MEF discovery errors");
                }

                CompositionConfiguration configuration = CompositionConfiguration.Create(catalog);

                if (!configuration.CompositionErrors.IsEmpty)
                {
                    // capture the errors in an array for easier debugging
                    var errors = configuration.CompositionErrors.SelectMany(e => e).ToArray();
                    foreach (var error in errors)
                    {
                        LoggingService.LogInfo("MEF composition error: " + error.Message);
                    }

                    // For now while we're still transitioning to VSMEF it's useful to work
                    // even if the composition has some errors. TODO: re-enable this.
                    //configuration.ThrowOnErrors ();
                }

                timer.Trace("Composition configured");

                var runtimeComposition = RuntimeComposition.CreateRuntimeComposition(configuration);
                return(runtimeComposition);
            }
        }
Example #18
0
        private async Task <ExportProvider> Compose(object parentInstance)
        {
            ExportProvider exportProvider = null;
            PartDiscovery  discovery      = PartDiscovery.Combine(
                new AttributedPartDiscovery(Resolver.DefaultInstance),
                new AttributedPartDiscoveryV1(Resolver.DefaultInstance)); // ".NET MEF" attributes (System.ComponentModel.Composition)

            Assembly parentAssembly = parentInstance.GetType().Assembly;
            string   parentLocation = parentAssembly.Location;
            string   assemblyPath   = parentLocation;

            assemblyPath = assemblyPath.Substring(0, assemblyPath.LastIndexOf('\\'));

            Helpers       desktopBridgeHelper = new Helpers();
            List <string> assemblies          = new[] { parentLocation }
            .Concat(
                Directory.EnumerateFiles(assemblyPath, "*.dll", SearchOption.TopDirectoryOnly)
                .Where(_ => _.Contains("NUnit3GUI")))
            .ToList();

            DiscoveredParts discoveredParts = await discovery.CreatePartsAsync(assemblies);

            discoveredParts.ThrowOnErrors();

            ComposableCatalog catalog = ComposableCatalog.Create(Resolver.DefaultInstance)
                                        .AddParts(discoveredParts)
                                        .WithCompositionService();

            CompositionConfiguration config = CompositionConfiguration.Create(catalog);

            config.ThrowOnErrors();

            IExportProviderFactory epf = config.CreateExportProviderFactory();

            exportProvider = epf.CreateExportProvider();

            ICompositionService service = exportProvider.GetExportedValue <ICompositionService>();

            service.SatisfyImportsOnce(parentInstance);

            return(exportProvider);
        }