Exemplo n.º 1
0
        private static void BuildArea(ref ModularityManifest modularity, ModularityArea area)
        {
            foreach (string module_id in area.Options.Modules)
            {
                var moduleConfig = modularity.Modules.Where(m => m.Id == module_id).FirstOrDefault();
                //todl: aynı name space in tekrar yüklenmesi durumu olabilir mi?

                if (moduleConfig == null)
                {
                    throw new Exception($"Module not found : {module_id}");
                }

                var directory     = GetAssemblyDirectory(moduleConfig, modularity.WorkingDirectory);
                var directoryInfo = new DirectoryInfo(directory);
                var module        = new ModularityModule(moduleConfig, modularity.ContentDirectory);

                if (!moduleConfig.Bundle && directoryInfo.Exists)
                {
                    FileSystemInfo[] files = directoryInfo.GetFileSystemInfos("*.dll", SearchOption.AllDirectories);

                    foreach (FileSystemInfo file in files)
                    {
                        //RequestPathPrefix not affected
                        if (Context.TryAddGetFileAlreadyLoad(file.Name))
                        {
                            continue;
                        }

                        Assembly assembly;

                        try
                        {
                            assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName);
                        }
                        catch (FileLoadException)
                        {
                            assembly = Assembly.Load(new AssemblyName(Path.GetFileNameWithoutExtension(file.Name)));

                            if (assembly == null)
                            {
                                throw;
                            }

                            string loadedAssemblyVersion    = FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion;
                            string tryToLoadAssemblyVersion = FileVersionInfo.GetVersionInfo(file.FullName).FileVersion;

                            if (tryToLoadAssemblyVersion != loadedAssemblyVersion)
                            {
                                throw new Exception($"Cannot load {file.FullName} {tryToLoadAssemblyVersion} because {assembly.Location} {loadedAssemblyVersion} has been loaded");
                            }
                        }

                        var id = Path.GetFileNameWithoutExtension(assembly.ManifestModule.Name);

                        if (id == moduleConfig.Id)
                        {
                            module.Assembly = assembly;
                        }
                    }
                }
                else
                {
                    module.Assembly = Assembly.Load(new AssemblyName(moduleConfig.Id));
                }

                if (module.Assembly == null)
                {
                    throw new Exception($"Cannot find main assembly for module {moduleConfig.Id}");
                }
                else
                {
                    module.Options.Installed = true;
                }

                area.AddModule(module);
            }

            Context.AddArea(area);

            if (area.IsDefaultArea)
            {
                Context.DefaultArea = area;
            }
        }