/// <summary> /// Registering method. /// </summary> /// <param name="e">Event raised.</param> private void OnRegistering(RegisteringEventArgs e) { if (Registering != null) { Registering(this, e); } }
internal void Register(IList <Tuple <string, string> > pathsToScan) { RegisteringEventArgs rea = new RegisteringEventArgs(); OnRegistering(rea); AggregateCatalog aggc = new AggregateCatalog(); List <DirectoryCatalog> catalogs = new List <DirectoryCatalog>(); List <ComposablePartDefinition> parts = new List <ComposablePartDefinition>(); foreach (var pathScan in pathsToScan) { string folder = String.IsNullOrEmpty(pathScan.Item1) ? System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) : pathScan.Item1; //Default if empty string path = System.IO.Path.Combine(folder, pathScan.Item2); //Full path if (!catalogs.Exists(x => System.IO.Path.Combine(x.FullPath.ToUpperInvariant(), x.SearchPattern).ToUpperInvariant() == path.ToUpperInvariant())) { //Check if same path is already loaded DirectoryCatalog dc = new DirectoryCatalog(folder, pathScan.Item2); if (dc.Parts.All(x => parts.Exists(y => y.ToString() == x.ToString()))) {//Check all parts, same assembly with different name or same assembly in two differents locations dc.Dispose(); } else { parts.AddRange(dc.Parts); if (dc.LoadedFiles.Count() > 0) {//Only add if assemblies loaded catalogs.Add(dc); for (int i = 0; i < catalogs.IndexOf(dc); i++) { if (catalogs.ElementAt(i).LoadedFiles.Intersect(dc.LoadedFiles).Count() > 0) { //There are similar files if (catalogs.ElementAt(i).LoadedFiles.Count() > dc.LoadedFiles.Count()) { //One of them could have more files than another, take the higher catalogs.Remove(dc); } else { catalogs.Remove(catalogs.ElementAt(i)); } } } } } } } catalogs.ForEach(x => aggc.Catalogs.Add(x)); aggc.Catalogs.Add(rea.TypeCatalog); ContainerSimple = new CompositionContainer(aggc, CompositionOptions.DisableSilentRejection | CompositionOptions.IsThreadSafe); var scopeDefDependent = new CompositionScopeDefinition(ContainerSimple.Catalog, null); var appCatalog = new TypeCatalog(typeof(ApplicationFactory <>)); var scopeDefRoot = new CompositionScopeDefinition(appCatalog, new[] { scopeDefDependent }); ContainerFactory = new CompositionContainer(scopeDefRoot, CompositionOptions.IsThreadSafe); OnRegistered(new RegisteredEventArgs(this.ContainerSimple, this.ContainerFactory)); }