public static ICollection <ProviderHandler> LoadProvider(params DirectoryInfo[] probingDirectories) { if (s_providers == null || s_providers.Count == 0) { // Initialize a list that will contain all plugin types discovered s_providers = new List <ProviderHandler>(); if (probingDirectories != null) { // Iterate over the probing directories and look for plugins foreach (DirectoryInfo directory in probingDirectories) { Debug.Assert(directory.Exists, string.Format("Plugins directory does not exist: {0}", directory.FullName)); if (directory.Exists) { // Try to load plugins from each dll foreach (FileInfo file in directory.GetFiles("*.dll")) { try { // Load the dll into an assembly Assembly assembly = Assembly.LoadFrom(file.FullName); // Iterate over all types contained in the assembly foreach (Type type in assembly.GetTypes()) { // Only consider public, concrete types that implement IProvider if (type.IsPublic && !type.IsAbstract && (type.GetInterface(typeof(IProvider).Name) != null)) { ProviderHandler handler = ProviderHandler.FromType(type); if (null == handler || handler.ProviderId == null || handler.ProviderId == Guid.Empty) { continue; } s_providers.Add(handler); TraceManager.TraceInformation("Provider {0} {1} is available", handler.ProviderName, handler.ProviderId.ToString()); } } } catch (ReflectionTypeLoadException) { TraceManager.TraceInformation("Provider {0} is unavailable. One or more of the requested types cannot be loaded.", file.FullName); } catch (Exception) { // We try to load all .dll files and some of them just are not providers. // Just log a warning and move along TraceManager.TraceWarning("Failed to load the possible provider file: {0}", file.FullName); } } } } } } return(s_providers); }
internal ProviderHandler(ProviderHandler fromCopy) { m_type = fromCopy.m_type; m_descriptionAttribute = fromCopy.m_descriptionAttribute; m_capabilityAttribute = fromCopy.m_capabilityAttribute; m_provider = (IProvider)Activator.CreateInstance(m_type); m_internalId = fromCopy.InternalId; VersionChanged = fromCopy.VersionChanged; }
/// <summary> /// Discovers providers under the specified directories and loads them if used in MigrationSources /// defined in the configuration. It instantiates a unique provider instance for each MigrationSource. /// </summary> public Dictionary <Guid, ProviderHandler> LoadProvider(params DirectoryInfo[] probingDirectories) { IEnumerable <ProviderHandler> providers = Utility.LoadProvider(probingDirectories); // Initialize a list that will contain all plugin types discovered Dictionary <Guid, ProviderHandler> providerHandlers = new Dictionary <Guid, ProviderHandler>(); foreach (ProviderHandler handler in providers) { try { #region Register Add-Ins IProvider provider = handler.Provider; IAddin addIn = provider.GetService(typeof(IAddin)) as IAddin; if (null != addIn) { m_AddinManagementService.RegisterAddin(addIn); } #endregion Guid[] sourceIds = GetMigrationSourceId(handler.ProviderId); if (sourceIds == null || sourceIds.Length == 0) { continue; } // try persist provider information to db handler.FindSaveProvider(); // create a unique provider instance for each migration source foreach (Guid migrationSource in sourceIds) { ProviderHandler providerHandler = new ProviderHandler(handler); if (!providerHandlers.ContainsKey(migrationSource)) { providerHandlers.Add(migrationSource, providerHandler); TraceManager.TraceInformation("Provider {0} {1} is loaded", providerHandler.ProviderName, handler.ProviderId.ToString()); } } } catch (Exception ex) { TraceManager.TraceError("A failure occurred while trying to load the {0} Provider: {1}{2}", handler.ProviderName, Environment.NewLine, ex.ToString()); } } return(providerHandlers); }