Ejemplo n.º 1
0
        internal static void LoadModules(IConfiguration configuration)
        {
            string depFolder = configuration[CommandLineFlags.DebugModuleFolder];

            if (depFolder != null)
            {
                if (Path.IsPathFullyQualified(depFolder))
                {
                    _moduleFolder = depFolder;
                }
                else
                {
                    _moduleFolder = Path.Combine(Directory.GetCurrentDirectory(), depFolder);
                }
            }
            else if (!Directory.Exists(_moduleFolder))
            {
                Directory.CreateDirectory(_moduleFolder);
            }

            AppDomain.CurrentDomain.AssemblyResolve += ResolveMissingDependency;

            IEnumerable <string> dlls = Directory.EnumerateFiles(_moduleFolder);

            PossibleDependencies = dlls.Where(file => file.EndsWith(".dll") && !file.EndsWith("Module.dll"));

            string loadModules = configuration[CommandLineFlags.DebugLoadModules];

            if (loadModules != null)
            {
                dlls = dlls.Concat(loadModules.Split(",")).Distinct();
            }

            ModuleAssemblies = LoadAssemblies(dlls.Where(file => file.EndsWith("Module.dll")));

            string environmentName = EnvironmentHelper.GetEnvironmentName();

            PossibleConfigFiles = ModuleAssemblies.SelectMany(item =>
            {
                string configBaseName = Path.Combine(Path.GetDirectoryName(item.Location), $"appsettings.{Path.GetFileNameWithoutExtension(item.Location)}.");
                return(new string[] { configBaseName + "json", configBaseName + environmentName + ".json" });
            });

            string addConfigs = configuration[CommandLineFlags.DebugAddConfigFiles];

            if (addConfigs != null)
            {
                PossibleConfigFiles = PossibleConfigFiles.Concat(addConfigs.Split(",")).Distinct();
            }

            DependencyInjectedTypes = ModuleAssemblies
                                      .Concat(new List <Assembly> {
                Assembly.GetExecutingAssembly()
            })
                                      .SelectMany(assembly => assembly.GetTypes())
                                      .Where(type => type.IsDefined(typeof(DependencyInjectedAttribute), false));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Load all running assemblies and solution assemlblies
        /// </summary>
        public void ReloadAssemblies()
        {
            this.ModuleAssemblies = new List <string>();

            var currentAssemblies = AppDomain.CurrentDomain
                                    .GetAssemblies()
                                    .Where(a => !a.FullName.StartsWith("System.") &&
                                           !a.FullName.StartsWith("Microsoft.") &&
                                           !a.FullName.StartsWith("mscorlib,") &&
                                           !a.FullName.StartsWith("Antlr3.") &&
                                           !a.FullName.StartsWith("System,") &&
                                           !a.FullName.StartsWith("CSharpFormat,") &&
                                           !a.FullName.StartsWith("ICSharpCode.") &&
                                           !a.FullName.StartsWith("NuGet.") &&
                                           !a.FullName.StartsWith("EntityFramework,") &&
                                           !a.FullName.StartsWith("Newtonsoft") &&
                                           !a.FullName.StartsWith("MySql.") &&
                                           !a.FullName.StartsWith("WebGrease,") &&
                                           !a.FullName.StartsWith("WebMatrix.")
                                           )
                                    .ToList();

            var bin           = HostingEnvironment.MapPath("~/content/modules/");
            var assemblyFiles = Directory.GetFiles(bin, "*.dll", SearchOption.AllDirectories);

            var asms = assemblyFiles.Select(f => new FileInfo(f))
                       .Where(s => !s.Directory.Name.Equals("Bin", StringComparison.OrdinalIgnoreCase) &&
                              !s.Directory.Name.Equals("Debug", StringComparison.OrdinalIgnoreCase) &&
                              !s.Directory.Name.Equals("Temp", StringComparison.OrdinalIgnoreCase) &&
                              !s.Name.StartsWith("System.", StringComparison.OrdinalIgnoreCase) &&
                              !s.Name.StartsWith("Microsoft.", StringComparison.OrdinalIgnoreCase) &&
                              !s.Name.StartsWith("EntityFramework.", StringComparison.OrdinalIgnoreCase) &&
                              !s.Name.StartsWith("Newtonsoft.", StringComparison.OrdinalIgnoreCase) &&
                              !s.Name.StartsWith("Antlr3.", StringComparison.OrdinalIgnoreCase) &&
                              !s.Name.StartsWith("WebGrease.", StringComparison.OrdinalIgnoreCase) &&
                              !s.Name.StartsWith("CSharpFormat.", StringComparison.OrdinalIgnoreCase)
                              ).ToList();

            if (asms.Count() > 0)
            {
                foreach (var file in asms)
                {
                    var asm = Assembly.LoadFrom(file.FullName);
                    if (asm != null)
                    {
                        if (currentAssemblies.Count(a => a.FullName.Equals(asm.FullName)) == 0)
                        {
                            currentAssemblies.Add(asm);
                            ModuleAssemblies.Add(asm.FullName);
                        }
                    }
                }
            }
            Assemblies = currentAssemblies;
        }