Esempio n. 1
0
        /// <summary>
        /// Runs migrations from the specified assemblies.
        /// </summary>
        public void MigrateStructure(IList <ModuleDescriptor> moduleDescriptors)
        {
            var versions             = new Dictionary <long, IList <ModuleDescriptor> >();
            var moduleWithMigrations = new Dictionary <ModuleDescriptor, IList <Type> >();

            foreach (var moduleDescriptor in moduleDescriptors)
            {
                var migrationTypes = assemblyLoader.GetLoadableTypes(moduleDescriptor.GetType().Assembly, typeof(Migration));
                if (migrationTypes != null)
                {
                    var types = migrationTypes as IList <Type> ?? migrationTypes.ToList();
                    moduleWithMigrations.Add(moduleDescriptor, types);

                    foreach (var migrationType in types)
                    {
                        var migrationAttributes = migrationType.GetCustomAttributes(typeof(MigrationAttribute), true);
                        if (migrationAttributes.Length > 0)
                        {
                            var attribute = migrationAttributes[0] as MigrationAttribute;
                            if (attribute != null)
                            {
                                if (!versions.ContainsKey(attribute.Version))
                                {
                                    versions[attribute.Version] = new List <ModuleDescriptor>();
                                }
                                versions[attribute.Version].Add(moduleDescriptor);
                            }
                        }
                    }
                }
            }

            foreach (var version in versions.OrderBy(f => f.Key))
            {
                var versionNumber = version.Key;

                foreach (var moduleDescriptor in version.Value)
                {
                    if (!versionChecker.VersionExists(moduleDescriptor.Name, versionNumber))
                    {
                        var migrationTypes = moduleWithMigrations[moduleDescriptor];
                        Migrate(moduleDescriptor, migrationTypes, versionNumber);

                        versionChecker.AddVersion(moduleDescriptor.Name, versionNumber);
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Tries to scan and adds module descriptor type from assembly.
        /// </summary>
        /// <param name="assembly">The assembly to scan.</param>
        public void AddModuleDescriptorTypeFromAssembly(Assembly assembly)
        {
            if (Log.IsTraceEnabled)
            {
                Log.TraceFormat("Searching for module descriptor type in the assembly {0}.", assembly.FullName);
            }

            var moduleRegistrationType = assemblyLoader.GetLoadableTypes(assembly).Where(IsModuleDescriptorType).FirstOrDefault();

            if (moduleRegistrationType != null)
            {
                if (Log.IsTraceEnabled)
                {
                    Log.TraceFormat("Adds module descriptor {0} from the assembly {1}.", moduleRegistrationType.Name, assembly.FullName);
                }

                if (!knownModuleDescriptorTypes.ContainsKey(moduleRegistrationType.Name))
                {
                    knownModuleDescriptorTypes.Add(moduleRegistrationType.Name, moduleRegistrationType);
                }
                else
                {
                    Log.InfoFormat("Module descriptor {0} from the assembly {1} already included.", moduleRegistrationType.Name, assembly.FullName);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Runs migrations from the specified assemblies.
        /// </summary>
        public void MigrateStructure(IList <ModuleDescriptor> moduleDescriptors)
        {
            IList <long> versions = new List <long>();
            IDictionary <ModuleDescriptor, IList <Type> > moduleWithMigrations = new Dictionary <ModuleDescriptor, IList <Type> >();

            foreach (var moduleDescriptor in moduleDescriptors)
            {
                var migrationTypes = assemblyLoader.GetLoadableTypes(moduleDescriptor.GetType().Assembly, typeof(Migration));
                if (migrationTypes != null)
                {
                    var types = migrationTypes as IList <Type> ?? migrationTypes.ToList();
                    moduleWithMigrations.Add(moduleDescriptor, types);

                    foreach (var migrationType in types)
                    {
                        var migrationAttributes = migrationType.GetCustomAttributes(typeof(MigrationAttribute), true);
                        if (migrationAttributes.Length > 0)
                        {
                            var attribute = migrationAttributes[0] as MigrationAttribute;
                            if (attribute != null)
                            {
                                versions.Add(attribute.Version);
                            }
                        }
                    }
                }
            }

            versions = versions.OrderBy(f => f).ToList();

            foreach (var version in versions)
            {
                foreach (var moduleWithMigration in moduleWithMigrations)
                {
                    if (!versionChecker.VersionExists(moduleWithMigration.Key.Name, version))
                    {
                        Migrate(moduleWithMigration.Key, moduleWithMigration.Value, version);
                        versionChecker.AddVersion(moduleWithMigration.Key.Name, version);
                    }
                }
            }
        }
        /// <summary>
        /// Gets the controller types from given assembly.
        /// </summary>
        /// <typeparam name="TController">The type of the controller.</typeparam>
        /// <param name="assembly">The assembly.</param>
        /// <returns>
        /// Controller types from assembly.
        /// </returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public IEnumerable <Type> GetControllerTypes <TController>(Assembly assembly)
        {
            var types = assemblyLoader.GetLoadableTypes(assembly);

            if (types != null)
            {
                foreach (var type in types)
                {
                    if (IsControllerType <TController>(type))
                    {
                        yield return(type);
                    }
                }
            }
        }
Esempio n. 5
0
        private static Assembly[] GetAssembliesWithServices()
        {
            List <Assembly>         assemblies = new List <Assembly>();
            ICmsModulesRegistration modulesRegistry;
            IAssemblyLoader         assemblyLoader = null;

            using (var container = ContextScopeProvider.CreateChildContainer())
            {
                modulesRegistry = container.Resolve <ICmsModulesRegistration>();
                if (modulesRegistry == null)
                {
                    throw new CmsApiException("Failed to resolve ICmsModulesRegistration.");
                }

                assemblyLoader = container.Resolve <IAssemblyLoader>();
                if (assemblyLoader == null)
                {
                    throw new CmsApiException("Failed to resolve IAssemblyLoader.");
                }

                foreach (var module in modulesRegistry.GetModules())
                {
                    try
                    {
                        var assembly = assemblyLoader.Load(module.ModuleDescriptor.AssemblyName);
                        if (assembly != null)
                        {
                            var types = assemblyLoader.GetLoadableTypes(assembly);
                            foreach (var type in types)
                            {
                                if (typeof(ServiceStack.ServiceInterface.Service).IsAssignableFrom(type) && type != null && type.IsPublic && type.IsClass &&
                                    !type.IsAbstract)
                                {
                                    assemblies.Add(assembly);
                                    break;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.ErrorFormat("Failed to check for ServiceStack services in the assembly {0}.", ex, module.ModuleDescriptor.AssemblyName);
                    }
                }
            }

            return(assemblies.ToArray());
        }