Ejemplo n.º 1
0
        /// <summary>
        /// Return the full path of the executing application (here we assume that ModuleManager.dll is in that path) and concatenate the assembly name of the module.
        /// .NET requires the the full path in order to load the associated assembly.
        /// </summary>
        protected virtual FullPath GetFullPath(AssemblyFileName assemblyName, OptionalPath optionalPath)
        {
            string appLocation;
            string assyLocation = Assembly.GetExecutingAssembly().Location;

            // An assembly that is loaded as a resource will have its assembly location as "".
            if (assyLocation == "")
            {
                Assert.Not(optionalPath == null, "Assemblies embedded as resources require that the optionalPath parameter specify the path to resolve assemblies.");
                appLocation = optionalPath.Value;                       // Must be specified!  Here the optional path is the full path?  This gives two different meanings to how optional path is used!
            }
            else
            {
                appLocation = Path.GetDirectoryName(assyLocation);

                if (optionalPath != null)
                {
                    appLocation = Path.Combine(appLocation, optionalPath.Value);
                }
            }

            string fullPath = Path.Combine(appLocation, assemblyName.Value);

            return(FullPath.Create(fullPath));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Register modules specified in a list of assembly filenames.
        /// </summary>
        public virtual void RegisterModules(List <AssemblyFileName> moduleFilenames, OptionalPath optionalPath = null, Func <string, Assembly> assemblyResolver = null)
        {
            List <Assembly> modules     = LoadModules(moduleFilenames, optionalPath, assemblyResolver);
            List <IModule>  registrants = InstantiateRegistrants(modules);

            InitializeRegistrants(registrants);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Return the full path of the executing application (here we assume that ModuleManager.dll is in that path) and concatenate the assembly name of the module.
        /// .NET requires the the full path in order to load the associated assembly.
        /// </summary>
        protected virtual FullPath GetFullPath(AssemblyFileName assemblyName, OptionalPath optionalPath)
        {
            string appLocation;
            string assyLocation = Assembly.GetExecutingAssembly().Location;

            // An assembly that is loaded as a resource will have its assembly location as "".
            if (assyLocation == "")
            {
                Assert.Not(optionalPath == null, "Assemblies embedded as resources require that the optionalPath parameter specify the path to resolve assemblies.");
                appLocation = optionalPath.Value;       // Must be specified!  Here the optional path is the full path?  This gives two different meanings to how optional path is used!
            }
            else
            {
                appLocation = Path.GetDirectoryName(assyLocation);

                if (optionalPath != null)
                {
                    appLocation = Path.Combine(appLocation, optionalPath.Value);
                }
            }

            string fullPath = Path.Combine(appLocation, assemblyName.Value);

            return FullPath.Create(fullPath);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            IModuleManager          mgr         = new ModuleManager();
            List <AssemblyFileName> moduleNames = GetModuleList(XmlFileName.Create("modules.xml"));

            mgr.RegisterModules(moduleNames, OptionalPath.Create("dll"));

            // The one and only module that is being loaded.
            IModule module = mgr.Modules[0];

            module.Say("Hello World.");
        }
Ejemplo n.º 5
0
        static void Bootstrap()
        {
            serviceManager = new ServiceManager();
            serviceManager.RegisterSingleton <IServiceModuleManager, ServiceModuleManager>();

            try
            {
                IModuleManager          moduleMgr = (IModuleManager)serviceManager.Get <IServiceModuleManager>();
                List <AssemblyFileName> modules   = GetModuleList(XmlFileName.Create("modules.xml"));
                moduleMgr.RegisterModules(modules, OptionalPath.Create("dll"));
                serviceManager.FinishedInitialization();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Initialization Error: " + ex.Message);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Load and return an assembly given the assembly filename so we can proceed with
        /// instantiating the module and so the module can register its services.
        /// </summary>
        protected virtual Assembly LoadAssembly(AssemblyFileName assyName, OptionalPath optionalPath, Func <string, Assembly> assemblyResolver)
        {
            FullPath fullPath = GetFullPath(assyName, optionalPath);
            Assembly assembly = null;

            if (!File.Exists(fullPath.Value))
            {
                Assert.Not(assemblyResolver == null, "Module " + fullPath.Value + " not found.\r\n.  An assemblyResolver must be defined when attempting to load modules from the application's resources or specify the optionalPath to locate the assembly.");
                assembly = assemblyResolver(assyName.Value);
            }
            else
            {
                try
                {
                    assembly = Assembly.LoadFile(fullPath.Value);
                }
                catch (Exception ex)
                {
                    throw new ModuleManagerException("Unable to load module " + assyName.Value + ": " + ex.Message);
                }
            }

            return(assembly);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Load the assemblies and return the list of loaded assemblies.  In order to register
        /// services that the module implements, we have to load the assembly.
        /// </summary>
        protected virtual List <Assembly> LoadModules(List <AssemblyFileName> moduleFilenames, OptionalPath optionalPath, Func <string, Assembly> assemblyResolver)
        {
            List <Assembly> modules = new List <Assembly>();

            moduleFilenames.ForEach(a =>
            {
                Assembly assembly = LoadAssembly(a, optionalPath, assemblyResolver);
                modules.Add(assembly);
            });

            return(modules);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Register modules specified in a list of assembly filenames.
 /// </summary>
 public virtual void RegisterModules(List<AssemblyFileName> moduleFilenames, OptionalPath optionalPath = null, Func<string, Assembly> assemblyResolver = null)
 {
     List<Assembly> modules = LoadModules(moduleFilenames, optionalPath, assemblyResolver);
     List<IModule> registrants = InstantiateRegistrants(modules);
     InitializeRegistrants(registrants);
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Load the assemblies and return the list of loaded assemblies.  In order to register
        /// services that the module implements, we have to load the assembly.
        /// </summary>
        protected virtual List<Assembly> LoadModules(List<AssemblyFileName> moduleFilenames, OptionalPath optionalPath, Func<string, Assembly> assemblyResolver)
        {
            List<Assembly> modules = new List<Assembly>();

            moduleFilenames.ForEach(a =>
            {
                Assembly assembly = LoadAssembly(a, optionalPath, assemblyResolver);
                modules.Add(assembly);
            });

            return modules;
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Load and return an assembly given the assembly filename so we can proceed with
        /// instantiating the module and so the module can register its services.
        /// </summary>
        protected virtual Assembly LoadAssembly(AssemblyFileName assyName, OptionalPath optionalPath, Func<string, Assembly> assemblyResolver)
        {
            FullPath fullPath = GetFullPath(assyName, optionalPath);
            Assembly assembly = null;

            if (!File.Exists(fullPath.Value))
            {
                Assert.Not(assemblyResolver == null, "Module " + fullPath.Value + " not found.\r\n.  An assemblyResolver must be defined when attempting to load modules from the application's resources or specify the optionalPath to locate the assembly.");
                assembly = assemblyResolver(assyName.Value);
            }
            else
            {
                try
                {
                    assembly = Assembly.LoadFile(fullPath.Value);
                }
                catch (Exception ex)
                {
                    throw new ModuleManagerException("Unable to load module " + assyName.Value + ": " + ex.Message);
                }
            }

            return assembly;
        }