static void Main(string[] args) { InterceptionConfiguration cfg = new InterceptionConfiguration() .AddInterceptor(new DynamicProxyInterceptor(new LoggingInterceptor())); //GenericTypeCatalog typeCatalog = new GenericTypeCatalog(typeof(GenericClass<int>), typeof(IGenericInterface<>)); //AssemblyCatalog assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); //AggregateCatalog aggregateCatalog = new AggregateCatalog(assemblyCatalog, typeCatalog); //GenericCatalog genericCatalog = new GenericCatalog(aggregateCatalog); //InterceptingCatalog interceptingCatalog = new InterceptingCatalog(genericCatalog, cfg); //CompositionContainer container = new CompositionContainer(interceptingCatalog); // GenericContractTypeMapping mapping = new GenericContractTypeMapping(typeof(IGenericInterface<>), typeof(GenericClass<>)); TypeCatalog typeCatalog = new TypeCatalog(typeof(SampleClass), typeof(OrderProcessor), typeof(CtorOrderProcessor)); AssemblyCatalog assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); GenericCatalog genericCatalog = new GenericCatalog(new TestGenericContractRegistry()); AggregateCatalog aggregateCatalog = new AggregateCatalog(typeCatalog, genericCatalog); //CompositionContainer container = new CompositionContainer(aggregateCatalog); CompositionContainer container = new CompositionContainer(aggregateCatalog); var orderProcessor = container.GetExportedValue <OrderProcessor>(); var generic = container.GetExportedValue <IGenericInterface <string> >(); CatalogExportProvider provider = new CatalogExportProvider(aggregateCatalog); provider.SourceProvider = provider; var instance = provider.GetExportedValue <ISampleInterface>();//container.GetExportedValue<ISampleInterface>(); instance.Method1("test", 5); var genericInstance = provider.GetExportedValue <OrderProcessor>();//container.GetExportedValue<IGenericInterface<int>>(); //var orderProcessor = provider.GetExportedValue<CtorOrderProcessor>(); }
public void Recomposition_PartDefWithNonRecomposableImportIsRemoved_ExportsMatchingImportChanged_ShouldNotBeRejected() { string dependencyContractName = "dependency"; var exportValue = new object(); var exporterPart = PartFactory.CreateExporter(dependencyContractName, exportValue); var importerPart = PartFactory.CreateImporter(dependencyContractName, false); var exporterCatalog = CatalogFactory.Create(exporterPart); var importerCatalog = CatalogFactory.Create(importerPart); var aggregateCatalog = CatalogFactory.CreateAggregateCatalog(importerCatalog, exporterCatalog); var provider = new CatalogExportProvider(aggregateCatalog); provider.SourceProvider = provider; var exports = provider.GetExports(importerPart.ImportDefinitions.Single()); Assert.AreEqual(exportValue, importerPart.Value, "Importer was not composed"); aggregateCatalog.Catalogs.Remove(importerCatalog); aggregateCatalog.Catalogs.Remove(exporterCatalog); Assert.AreEqual(exportValue, importerPart.Value, "Importer was unexpectedly recomposed"); }
public MefExportProvider(ComposablePartCatalog catalog) { _exportProvider = new CatalogExportProvider(catalog); //support recomposition _exportProvider.ExportsChanged += (s, e) => OnExportsChanged(e); _exportProvider.ExportsChanging += (s, e) => OnExportsChanging(e); }
/// <summary> /// Create the composition container used to locate and fixup imports. /// </summary> private void CreateContainer() { var defaultAssembly = Path.GetFileName(Assembly.GetExecutingAssembly().CodeBase); var localCatalog = new CatalogExportProvider(new AssemblyCatalog(Assembly.GetExecutingAssembly())); // Get a catalog of all the assemblies *except* this one var catalog = new AggregateCatalog(); foreach (string fname in Directory.GetFiles(@".\", "*.exe") .Concat(Directory.GetFiles(@".\", "*.dll")) .Where(fname => string.Compare(Path.GetFileName(fname), defaultAssembly, true) != 0)) { try { if (Assembly.ReflectionOnlyLoadFrom(fname) != null) { catalog.Catalogs.Add(new AssemblyCatalog(fname)); } } catch { } } _container = new CompositionContainer(catalog, localCatalog); localCatalog.SourceProvider = _container; }
private CompositionContainer PrepareCompositionContainer() { // Create the MEF container based on the catalog and given providers // Important: the catalog is wrapped with CatalogExportProvider which is // then added as a LAST catalog, this ensures that when querying Unity/MEF // for a single component, Unity components will always take precedence var catalogExportProvider = new CatalogExportProvider(this.aggregateCatalog, this.isThreadSafe); var providerList = new List <ExportProvider>(this.providers); providerList.Add(catalogExportProvider); var container = new CompositionContainer(null, this.isThreadSafe, providerList.ToArray()); catalogExportProvider.SourceProvider = container; // If desired, register an instance of CompositionContainer and Unity container in MEF, // this will also make CompositionContainer available to the Unity if (Register) { // Create composition batch and add the MEF container and the Unity // container to the MEF var batch = new CompositionBatch(); batch.AddExportedValue(container); batch.AddExportedValue(Container); // Prepare container container.Compose(batch); } return(container); }
/// <summary> /// By default, we are configured to use MEF /// </summary> protected override void Configure() { // Add all assemblies to AssemblySource (using a temporary DirectoryCatalog). var directoryCatalog = new DirectoryCatalog(@"./"); AssemblySource.Instance.AddRange( directoryCatalog.Parts .Select(part => ReflectionModelServices.GetPartType(part).Value.Assembly) .Where(assembly => !AssemblySource.Instance.Contains(assembly))); // Prioritise the executable assembly. This allows the client project to override exports, including IShell. // The client project can override SelectAssemblies to choose which assemblies are prioritised. var priorityAssemblies = SelectAssemblies().ToList(); var priorityCatalog = new AggregateCatalog(priorityAssemblies.Select(x => new AssemblyCatalog(x))); var priorityProvider = new CatalogExportProvider(priorityCatalog); // Now get all other assemblies (excluding the priority assemblies). var mainCatalog = new AggregateCatalog( AssemblySource.Instance .Where(assembly => !priorityAssemblies.Contains(assembly)) .Select(x => new AssemblyCatalog(x))); var mainProvider = new CatalogExportProvider(mainCatalog); Container = new CompositionContainer(priorityProvider, mainProvider); priorityProvider.SourceProvider = Container; mainProvider.SourceProvider = Container; var batch = new CompositionBatch(); BindServices(batch); batch.AddExportedValue(mainCatalog); Container.Compose(batch); }
public void Configure() { var priorityAssemblies = SelectAssemblies().ToList(); var priorityCatalog = new AggregateCatalog(priorityAssemblies.Select(x => new AssemblyCatalog(x))); var priorityProvider = new CatalogExportProvider(priorityCatalog); var path = Path.GetDirectoryName( new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath); var search = "uDicom.PACS.Service.dll"; var codecCatalog = new DirectoryCatalog(path, search); var codecProvider = new CatalogExportProvider(codecCatalog); Container = new CompositionContainer(priorityProvider, codecProvider); priorityProvider.SourceProvider = Container; codecProvider.SourceProvider = Container; var batch = new CompositionBatch(); batch.AddExportedValue(Container); Container.Compose(batch); IoC.GetInstance = GetInstance; IoC.GetAllInstances = GetAllInstances; }
/// <inheritdoc /> /// <summary> /// Override to configure the framework and setup your IoC container. /// </summary> protected override void Configure() { // Add all assemblies to AssemblySource (using a temporary DirectoryCatalog). var directoryCatalog = new DirectoryCatalog(@"."); AssemblySource.Instance.AddRange( directoryCatalog.Parts .Select(part => ReflectionModelServices.GetPartType(part).Value.Assembly) .Where(assembly => !AssemblySource.Instance.Contains(assembly))); _priorityAssemblies = SelectAssemblies().ToList(); var priorityCatalog = new AggregateCatalog(_priorityAssemblies.Select(x => new AssemblyCatalog(x))); var priorityProvider = new CatalogExportProvider(priorityCatalog); var mainCatalog = new AggregateCatalog( AssemblySource.Instance .Where(assembly => !_priorityAssemblies.Contains(assembly)) .Select(x => new AssemblyCatalog(x))); var mainProvider = new CatalogExportProvider(mainCatalog); _container = new CompositionContainer(priorityProvider, mainProvider); priorityProvider.SourceProvider = _container; mainProvider.SourceProvider = _container; var batch = new CompositionBatch(); BindServices(batch); _container.Compose(batch); }
public void TestInitialize() { var exp = new CatalogExportProvider(Catalog = CreateCatalog()); var agg = new NancyExportProvider(exp); Container = new CompositionContainer(agg); exp.SourceProvider = agg; }
public void GetExports_NoSourceProvider_ShouldThrowInvalidOperation() { var catalog = CatalogFactory.CreateAttributed(); var provider = new CatalogExportProvider(catalog); ExceptionAssert.Throws <InvalidOperationException>(() => provider.GetExports(ImportFromContract("Foo"))); }
public AOPExportProvider(Func <ComposablePartCatalog> catalogResolver) { _exportProvider = new CatalogExportProvider(catalogResolver()); //support recomposition _exportProvider.ExportsChanged += (s, e) => OnExportsChanged(e); _exportProvider.ExportsChanging += (s, e) => OnExportsChanging(e); }
/// <summary>Creates and returns a new <see cref="CompositionContainer"/> to be used by <see langword="this"/>.</summary> /// <returns>A new <see cref="CompositionContainer"/> to be used by <see langword="this"/>.</returns> public CompositionContainer CreateCompositionContainer() { var exportProvider = new CatalogExportProvider(new AssemblyCatalog(GetType().Assembly)); var compositionContainer = new CompositionContainer(CompositionOptions.IsThreadSafe, exportProvider); exportProvider.SourceProvider = compositionContainer; return(compositionContainer); }
private void LoadRegistriesFromCatalog() { using (var ep = new CatalogExportProvider(this.decoratedCatalog)) { ep.SourceProvider = ep; var registries = ep.GetExportedValues <IGenericContractRegistry>(); LoadTypeMappings(registries); } }
/// <summary> /// This uses MEF to perform a compose operation. /// </summary> /// <typeparam name="T">Type to look for</typeparam> /// <param name="contractName">MEF contract name</param> /// <param name="assemblyPath">Assembly path</param> /// <param name="filter">Filter for files</param> /// <returns></returns> private static List <T> Compose <T>(string contractName, string assemblyPath, string filter) where T : class { if (container == null) { ComposablePartCatalog partCatalog; var assembly = Assembly.GetEntryAssembly(); if (assembly != null) { partCatalog = new AggregateCatalog( new AssemblyCatalog(assembly), GetCatalogForDirectoryFileSpec(AssemblyResolver.BioInstallationPath, DLLFilter)); } else { partCatalog = GetCatalogForDirectoryFileSpec(AssemblyResolver.BioInstallationPath, DLLFilter); } // Add the add-in folder if we have one if (!string.IsNullOrEmpty(assemblyPath) && Directory.Exists(assemblyPath)) { var addinCatalog = GetCatalogForDirectoryFileSpec(assemblyPath, filter); var defaultCatalogEp = new CatalogExportProvider(partCatalog); container = new CompositionContainer(addinCatalog, defaultCatalogEp); defaultCatalogEp.SourceProvider = container; } else { container = new CompositionContainer(partCatalog); } } List <T> availableTypes = new List <T>(); var exportList = container.GetExports <T>(contractName); foreach (Lazy <T> foundType in exportList) { try { T value = foundType.Value; if (value != null) { availableTypes.Add(value); } } catch (Exception ex) { // Ignore if can't create. Debug.WriteLine(ex.ToString()); } } return(availableTypes); }
public void Constructor_ValueAsCatalogArgument_ShouldSetCatalogPropertyToEmpty() { var expectations = Expectations.GetCatalogs(); foreach (var e in expectations) { var provider = new CatalogExportProvider(e); Assert.Same(e, provider.Catalog); } }
[ActiveIssue("https://github.com/dotnet/corefx/issues/25498", TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. public void ComponentCatalogResolverGetStaticExport() { var catalog = CatalogFactory.CreateDefaultAttributed(); var catalogExportProvider = new CatalogExportProvider(catalog); catalogExportProvider.SourceProvider = catalogExportProvider; var exports = catalogExportProvider.GetExports(ImportFromContract("StaticString")); Assert.Equal(1, exports.Count()); Assert.Equal("StaticString", exports.First().Value); }
public void TestSetUp() { var typeCatalog = new TypeCatalog(typeof(CtorOrderProcessor), typeof(OrderProcessor), typeof(TestGenericContractRegistry)); var cfg = new InterceptionConfiguration().AddHandler(new GenericExportHandler()); var catalog = new InterceptingCatalog(typeCatalog, cfg); var provider = new CatalogExportProvider(catalog); provider.SourceProvider = provider; ExportProvider = provider; }
public static CompositionContainer Create() { var productCatalog = new AggregateCatalog( new AssemblyCatalog(Assembly.Load("CodingArena.Game")), new AssemblyCatalog(Assembly.Load("CodingArena.Game.Console"))); var exportProvider = new CatalogExportProvider(productCatalog); var testCatalog = new AssemblyCatalog(Assembly.Load("CodingArena.Game.Tests")); var container = new CompositionContainer(testCatalog, exportProvider); exportProvider.SourceProvider = container; return(container); }
static NuPatternGlobalContainer() { var globalComponentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel)); if (globalComponentModel == null) { tracer.Warn(Resources.NuPatternGlobalContainer_TraceNoComponentModelService); } else { var catalog = globalComponentModel.GetCatalog(NuPattern.ComponentModel.Composition.Catalog.DefaultCatalogName); if (catalog == null) { tracer.Warn(Resources.NuPatternGlobalContainer_TraceNoComponentModel, NuPattern.ComponentModel.Composition.Catalog.DefaultCatalogName); } else { try { // // Create our DecoratingReflectionCatalog // var componentsCatalog = new ComponentCatalog(catalog); // // Now create a catalog provider // CatalogExportProvider catalogProvider = new CatalogExportProvider(componentsCatalog); // // have it inherit the global provider // catalogProvider.SourceProvider = globalComponentModel.DefaultExportProvider; // // Create provider settings to look here first and then include others when not found // var providerSettings = new VsExportProviderSettings( VsExportProvidingPreference.BeforeExportsFromOtherContainers, VsExportSharingPolicy.IncludeExportsFromOthers); //Instance = VsCompositionContainer.Create(componentsCatalog, providerSettings); Instance = new CompositionContainer(componentsCatalog, globalComponentModel.DefaultExportProvider); } catch (Exception ex) { tracer.Error(ex, Resources.NuPatternGlobalContainer_TraceFailedContainerInitialization); throw; } } } }
public static CompositionContainer Create() { var directoryCatalog = new DirectoryCatalog(AppDomain.CurrentDomain.BaseDirectory); var exportProvider = new CatalogExportProvider(directoryCatalog); var testCatalog = new AssemblyCatalog(Assembly.Load("Beeffective.Tests")); var container = new CompositionContainer(testCatalog, exportProvider); exportProvider.SourceProvider = container; return(container); }
[ActiveIssue("https://github.com/dotnet/corefx/issues/25498", TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. public void BasicTestWithRequiredMetadata_WrongTypeConstraint() { var catalog = CatalogFactory.CreateDefaultAttributed(); var catalogExportProvider = new CatalogExportProvider(catalog); catalogExportProvider.SourceProvider = catalogExportProvider; Assert.Equal(0, catalogExportProvider.GetExports(ImportFromContractAndMetadata("MyExporterWithNoFoo", new string[] { "Foo" }, new Type[] { typeof(int) })).Count()); Assert.Equal(0, catalogExportProvider.GetExports(ImportFromContractAndMetadata("MyExporterWithFoo", new string[] { "Foo" }, new Type[] { typeof(int) })).Count()); Assert.Equal(0, catalogExportProvider.GetExports(ImportFromContractAndMetadata("MyExporterWithFoo", new string[] { "Foo", "Bar" }, new Type[] { typeof(int), typeof(int) })).Count()); Assert.Equal(0, catalogExportProvider.GetExports(ImportFromContractAndMetadata("MyExporterWithNoFoo", new string[] { "Foo" }, new Type[] { typeof(int) })).Count()); Assert.Equal(0, catalogExportProvider.GetExports(ImportFromContractAndMetadata("MyExporterWithFoo", new string[] { "Foo" }, new Type[] { typeof(int) })).Count()); }
public void CreationPolicyShared_MultipleCallsReturnSameInstance() { var catalog = CatalogFactory.CreateAttributed(typeof(CreationPolicyShared)); var provider = new CatalogExportProvider(catalog); provider.SourceProvider = ContainerFactory.Create(); var export = provider.GetExportedValue <CreationPolicyShared>(); for (int i = 0; i < 5; i++) // 5 is arbitrarily chosen { var export1 = provider.GetExportedValue <CreationPolicyShared>(); Assert.Equal(export, export1); } }
[ActiveIssue("https://github.com/dotnet/corefx/issues/25498", TestPlatforms.AnyUnix)] // System.Reflection.ReflectionTypeLoadException : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. public void ComponentCatalogResolverComponentCatalogExportReference() { var catalog = CatalogFactory.CreateDefaultAttributed(); var catalogExportProvider = new CatalogExportProvider(catalog); catalogExportProvider.SourceProvider = catalogExportProvider; var exports = catalogExportProvider.GetExports(ImportFromContract(AttributedModelServices.GetContractName(typeof(MyExporterWithValidMetadata)))); Assert.Equal(1, exports.Count()); var export = exports.First(); Assert.Equal("world", export.Metadata["hello"]); Assert.IsType <MyExporterWithValidMetadata>(export.Value); }
/// <summary> /// 从当前目录下的所有程序集中加载所有实现 <see cref="IExportTargeterControlFactory"/> 接口的实例 /// </summary> public IList <IExportTargeterControlFactory> LoadIExportTargeterControlFactorys() { var providers = new List <IExportTargeterControlFactory>(); String directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); DirectoryCatalog catalog = new DirectoryCatalog(directory, SearchPattern); using (CatalogExportProvider exportProvider = new CatalogExportProvider(catalog)) { exportProvider.SourceProvider = exportProvider; var exports = exportProvider.GetExportedValues <IExportTargeterControlFactory>(); providers.AddRange(exports); } this.ExportTargeterControlFactorys = providers; return(providers); }
protected override void Configure() { var assemblies = SelectAssemblies(); var catalog = new AggregateCatalog(assemblies.Select(x => new AssemblyCatalog(x))); var provider = new CatalogExportProvider(catalog); m_Container = new CompositionContainer(provider); provider.SourceProvider = m_Container; var batch = new CompositionBatch(); batch.AddExportedValue <IWindowManager>(new WindowManager()); batch.AddExportedValue <IEventAggregator>(new EventAggregator()); batch.AddExportedValue(m_Container); batch.AddExportedValue(catalog); m_Container.Compose(batch); }
public IDynamicBindingContext CreateDynamicContext() { var bindingProvider = new CatalogExportProvider(this.bindingCatalog); var dynamicExports = new CompositionContainer(); var composition = new CompositionContainer( dynamicExports, defaultCompositionProvider, bindingProvider, componentModel.DefaultExportProvider); bindingProvider.SourceProvider = composition; var context = new CompositionContainerDynamicBindingContext(dynamicExports, composition); context.AddExport <IDynamicBindingContext>(context); return(context); }
protected override void Configure() { var priorityAssemblies = SelectAssemblies(); var priorityCatalog = new AggregateCatalog(priorityAssemblies.Select(x => new AssemblyCatalog(x))); var catalog = new CatalogExportProvider(priorityCatalog); _container = new CompositionContainer(catalog); catalog.SourceProvider = _container; var batch = new CompositionBatch(); BindServices(batch); batch.AddExportedValue(catalog); _container.Compose(batch); base.Configure(); }
public void TestSetUp() { var typeCatalog = new TypeCatalog( typeof(CtorOrderProcessor), typeof(OrderProcessor), typeof(ConcreteCtorOrderProcessor), typeof(ConcreteOrderProcessor), typeof(MyCtorOrderProcessor), typeof(MyOrderProcessor), typeof(MyOrderProcessorSetterOnly), typeof(MultiOrderProcessor)); var genericCatalog = new GenericCatalog(new TestGenericContractRegistry()); var aggregateCatalog = new AggregateCatalog(typeCatalog, genericCatalog); var provider = new CatalogExportProvider(aggregateCatalog); provider.SourceProvider = provider; ExportProvider = provider; }
private void CreateBindingContainer() { // We leverage the Feature Runtime container, where all their components and extensions publish already. var defaultCatalog = this.componentModel.GetCatalog(NuPattern.VisualStudio.Composition.Catalog.DefaultCatalogName); var ourCatalog = this.componentModel.GetCatalog(Catalog.DefaultCatalogName); if (defaultCatalog == null || ourCatalog == null) { throw new InvalidOperationException(Resources.BindingCompositionService_CatalogsNotAvailable); } try { // Transparently change the INuPatternCompositionService implementation for all components // without code changes by providing an instance earlier in the chain. // See http://codebetter.com/blogs/glenn.block/archive/2009/05/14/customizing-container-behavior-part-2-of-n-defaults.aspx this.defaultCompositionProvider = new CompositionContainer(); defaultCompositionProvider.ComposeExportedValue <INuPatternCompositionService>(this); defaultCompositionProvider.ComposeExportedValue <SVsServiceProvider>((SVsServiceProvider)serviceProvider); // Decorated catalog of parts. // NOTE: caching the catalog also caches the instantiated shared parts, if any. this.bindingCatalog = new BindingComponentCatalog(new AggregateCatalog(ourCatalog, defaultCatalog)); var bindingProvider = new CatalogExportProvider(this.bindingCatalog); this.container = new CompositionContainer( defaultCompositionProvider, bindingProvider, componentModel.DefaultExportProvider); bindingProvider.SourceProvider = this.container; } catch (Exception ex) { tracer.Error(ex, Resources.BindingCompositionService_FailedToInitialize); throw; } // Used by the CompositionServiceBindingContext to create a new CompositionContainer over // this one for dynamic context resolution. this.container.ComposeExportedValue <ExportProvider>(container); }
protected override CompositionContainer CreateInstance() { string extensionsPath = Path.Combine(SettingsStore.StoragePath, ExtensionDirectory); if (!Directory.Exists(ExtensionDirectory)) { Directory.CreateDirectory(ExtensionDirectory); } DirectoryCatalog thirdPartyCatelog = new DirectoryCatalog(ExtensionDirectory); AggregateCatalog defaultCatalog = new AggregateCatalog(); defaultCatalog.Catalogs.Add(new DirectoryCatalog("./", "Reshaper*.dll")); CatalogExportProvider defaultExportProvider = new CatalogExportProvider(defaultCatalog); CompositionContainer container = new CompositionContainer(thirdPartyCatelog, defaultExportProvider); defaultExportProvider.SourceProvider = container; return(container); }