Exemple #1
0
        public void when_constructing_container_then_can_retrieve_devenv()
        {
            var catalog = new ComponentCatalog(typeof(IDevEnv).Assembly);
            var container = new CompositionContainer(catalog, new ServicesExportProvider(base.ServiceProvider));
            IServiceLocator locator = null;

            // Make the service locator itself available as an export.
            var accessor = new Clide.DevEnvFactory.ServicesAccessor(base.ServiceProvider, 
                new Lazy<IServiceLocator>(() => locator));
            container.ComposeParts(accessor);

            locator = new ExportsServiceLocator(container);

            //var info = new CompositionInfo(catalog, container);
            //CompositionInfoTextFormatter.Write(info, Console.Out);

            var devenv = container.GetExportedValue<IDevEnv>();

            Assert.NotNull(devenv);
        }
        private IServiceLocator InitializeContainer(IServiceProvider services)
        {
            using (tracer.StartActivity(Strings.DevEnvFactory.CreatingComposition))
            {
                // Allow dependencies of VS exported services.
                var composition = services.GetService<SComponentModel, IComponentModel>();

                // Keep track of assemblies we've already added, to avoid duplicate registrations.
                var addedAssemblies = new Dictionary<string, Assembly>();

                // Register built-in components from Clide assembly.
                var clideAssembly = Assembly.GetExecutingAssembly();
                addedAssemblies.Add(clideAssembly.Location.ToLowerInvariant(), clideAssembly);

                // Register hosting package assembly.
                var servicesAssembly = services.GetType().Assembly;
                addedAssemblies[servicesAssembly.Location.ToLowerInvariant()] = servicesAssembly;

                var installPath = GetInstallPath(services);

                var packageManifestFile = Path.Combine(installPath, "extension.vsixmanifest");
                if (File.Exists(packageManifestFile))
                {
                    tracer.Info(Strings.DevEnvFactory.ExtensionManifestFound(packageManifestFile));
                    var manifestDoc = XDocument.Load(packageManifestFile);

                    ThrowIfClideIsMefComponent(manifestDoc);
                    // NOTE: we don't warn anymore in this case, since a single package may have
                    // a mix of plain VS exports as well as clide components.
                    // Since we use CommonComposition, only the types with the ComponentAttribute
                    // will be made available in the Clide container, not the others, and no
                    // duplicates would be registered.
                    //WarnIfClideComponentIsAlsoMefComponent(packageManifestFile, manifestDoc);

                    foreach (string clideComponent in GetClideComponents(manifestDoc))
                    {
                        var assemblyFile = Path.Combine(installPath, clideComponent);
                        tracer.Info(Strings.DevEnvFactory.ClideComponentDeclared(clideComponent, assemblyFile));

                        if (clideComponent == ClideAssembly)
                        {
                            tracer.Warn(Strings.DevEnvFactory.ClideNotNecessaryAsComponent(clideComponent));
                            continue;
                        }

                        if (!File.Exists(assemblyFile))
                            throw new InvalidOperationException(Strings.DevEnvFactory.ClideComponentNotFound(packageManifestFile, clideComponent, assemblyFile));

                        var componentAssembly = Assembly.LoadFrom(assemblyFile);
                        if (!addedAssemblies.ContainsKey(componentAssembly.Location.ToLowerInvariant()))
                            addedAssemblies.Add(componentAssembly.Location.ToLowerInvariant(), componentAssembly);
                    }
                }
                else
                {
                    tracer.Info(Strings.DevEnvFactory.ExtensionManifestNotFound(packageManifestFile));
                }

                var catalog = new ComponentCatalog(addedAssemblies.Values.ToArray());
                var container = new CompositionContainer(catalog,
                    composition.DefaultExportProvider,
                    new ServicesExportProvider(services));

                // Make the service locator itself available as an export.
                var serviceLocator = new ServicesAccessor(services, new Lazy<IServiceLocator>(() => serviceLocators[services]));
                container.ComposeParts(serviceLocator);

                return new ExportsServiceLocator(container);
            }
        }