public void ConfigureServices(IDependencyContainer container) { container.AddSingleton <ITaskScheduler, DefaultTaskScheduler>(); container.AddTransient <IConfiguration, YamlConfiguration>("yaml", null); container.AddTransient <IConfiguration, JsonConfiguration>("json"); container.AddTransient <IConfiguration, XmlConfiguration>("xml"); container.AddSingleton <IRocketConfigurationProvider, RocketConfigurationProvider>(); container.AddSingleton <ILogger, ConsoleLogger>("console_logger"); container.AddSingleton <ILogger, LoggerProxy>("proxy_logger", null); container.AddSingleton <IEventBus, EventBus>(); container.AddSingleton <ICommandHandler, DefaultCommandHandler>("default_cmdhandler"); container.AddSingleton <ICommandHandler, CommandHandlerProxy>("proxy_cmdhandler", null); container.AddSingleton <IPluginLoader, DefaultClrPluginLoader>("dll_plugins"); container.AddSingleton <IPluginLoader, NuGetPluginLoader>("nuget_plugins"); container.AddSingleton <IPluginLoader, PluginLoaderProxy>("proxy_plugins", null); container.AddSingleton <ICommandProvider, RocketCommandProvider>("rocket_cmdprovider"); container.AddSingleton <ICommandProvider, CommandProviderProxy>("proxy_cmdprovider", null); var configPermissions = container.Activate <ConfigurationPermissionProvider>(); container.AddTransient <IPermissionProvider>(configPermissions, "default_permissions", null); container.AddTransient <IPermissionChecker>(configPermissions, "default_permissions"); container.AddSingleton <IPermissionChecker, ConsolePermissionChecker>("console_checker"); container.AddSingleton <IPermissionChecker, PermissionProviderProxy>("proxy_checker", null); container.AddTransient <ITranslationCollection, TranslationCollection>(); container.AddSingleton <IUserManager, UserManagerProxy>(); }
public void Register(IDependencyContainer container, IDependencyResolver resolver) { container.RegisterType <IConfiguration, YamlConfiguration>("yaml", null); container.RegisterType <IConfiguration, JsonConfiguration>("json"); container.RegisterType <IConfiguration, XmlConfiguration>("xml"); container.RegisterSingletonType <IRocketConfigurationProvider, RocketConfigurationProvider>(); container.RegisterSingletonType <ILogger, ConsoleLogger>("console_logger"); container.RegisterSingletonType <ILogger, LoggerProxy>("proxy_logger", null); container.RegisterSingletonType <IEventBus, EventBus>(); container.RegisterSingletonType <ICommandHandler, DefaultCommandHandler>("default_cmdhandler"); container.RegisterSingletonType <ICommandHandler, CommandHandlerProxy>("proxy_cmdhandler", null); container.RegisterSingletonType <IPluginLoader, DefaultClrPluginLoader>("dll_plugins"); container.RegisterSingletonType <IPluginLoader, NuGetPluginLoader>("nuget_plugins"); container.RegisterSingletonType <IPluginLoader, PluginLoaderProxy>("proxy_plugins", null); container.RegisterSingletonType <ICommandProvider, RocketCommandProvider>("rocket_cmdprovider"); container.RegisterSingletonType <ICommandProvider, CommandProviderProxy>("proxy_cmdprovider", null); var configPermissions = container.Activate <ConfigurationPermissionProvider>(); container.RegisterInstance <IPermissionProvider>(configPermissions, "default_permissions", null); container.RegisterInstance <IPermissionChecker>(configPermissions, "default_permissions"); container.RegisterSingletonType <IPermissionChecker, ConsolePermissionChecker>("console_checker"); container.RegisterSingletonType <IPermissionChecker, PermissionProviderProxy>("proxy_checker", null); container.RegisterType <ITranslationCollection, TranslationCollection>(); container.RegisterSingletonType <IUserManager, UserManagerProxy>(); }
protected virtual IPlugin LoadPluginFromAssembly(Assembly pluginAssembly, out IDependencyContainer childContainer) { childContainer = null; string loc = GetAssemblyLocationSafe(pluginAssembly); if (!string.IsNullOrEmpty(loc) && !cachedAssemblies.ContainsKey(loc)) { cachedAssemblies.Add(loc, pluginAssembly); } Type[] types; try { types = pluginAssembly.GetTypes(); } catch (ReflectionTypeLoadException e) { types = e.Types; } Type pluginType = null; foreach (Type type in types.Where(t => t != null)) { if (type.IsAbstract || type.IsInterface) { continue; } if (type.GetCustomAttributes(typeof(DontAutoRegisterAttribute), true).Any()) { continue; } if (pluginType == null && typeof(IPlugin) != type && typeof(IPlugin).IsAssignableFrom(type)) { pluginType = type; } } if (pluginType == null) { return(null); } childContainer = Container.CreateChildContainer(); childContainer.RegisterInstance <IPluginManager>(this); IPlugin pluginInstance = (IPlugin)childContainer.Activate(pluginType); if (pluginInstance == null) { throw new Exception("Failed to activate: " + pluginType.FullName + ". Is your plugin constructor public?"); } Container.RegisterInstance(pluginInstance, pluginInstance.Name); childContainer.RegisterInstance(pluginInstance); IEnumerable <Type> pluginCommands = pluginInstance.FindTypes <ICommand>(false, c => !typeof(IChildCommand).IsAssignableFrom(c) && c.GetCustomAttributes(typeof(DontAutoRegisterAttribute), true).Length == 0); IEnumerable <Type> dependencyRegistrators = pluginInstance.FindTypes <IDependencyRegistrator>(false); foreach (Type registrator in dependencyRegistrators) { ((IDependencyRegistrator)Activator.CreateInstance(registrator)).Register(Container, Container); } foreach (Type command in pluginCommands) { try { ICommand cmdInstance = (ICommand)childContainer.Activate(command); if (cmdInstance != null) { childContainer.RegisterSingletonInstance(cmdInstance, cmdInstance.Name); } } catch (Exception ex) { Logger.LogError(null, ex); } } return(pluginInstance); }
protected async Task <bool> RegisterAndLoadPluginFromContainer(IDependencyContainer container) { IPlugin plugin = container.Resolve <IPlugin>(); Logger.LogDebug($"[{GetType().Name}] Trying to load plugin: " + plugin.Name); PluginCommandProvider cmdProvider = new PluginCommandProvider(plugin); ParentContainer.RegisterSingletonInstance <ICommandProvider>(cmdProvider, plugin.Name); var asm = plugin.GetType().Assembly; string pluginDir = plugin.WorkingDirectory; //if (!Directory.Exists(pluginDir)) // Directory.CreateDirectory(pluginDir); foreach (string s in asm.GetManifestResourceNames()) { using (Stream stream = asm.GetManifestResourceStream(s)) { using (MemoryStream ms = new MemoryStream()) { if (stream != null) { stream.CopyTo(ms); byte[] data = ms.ToArray(); string fileName = s.Replace(plugin.GetType().Namespace, ""); File.WriteAllBytes(Path.Combine(pluginDir, fileName), data); } } } } bool success = await plugin.ActivateAsync(false); if (!success) { return(false); } IEnumerable <Type> listeners = plugin.FindTypes <IAutoRegisteredEventListener>(false); foreach (Type listener in listeners) { IAutoRegisteredEventListener instance = (IAutoRegisteredEventListener)container.Activate(listener); EventBus.AddEventListener(plugin, instance); } return(true); }