Example #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);
        }
Example #2
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;
        }