예제 #1
0
        /// <summary>
        /// Registers the plugin's services with the DI.
        /// Note: DI is not yet instantiated yet.
        /// </summary>
        /// <param name="serviceCollection">A <see cref="ServiceCollection"/> instance.</param>
        public void RegisterServices(IServiceCollection serviceCollection)
        {
            foreach (var pluginServiceRegistrator in _appHost.GetExportTypes <IPluginServiceRegistrator>())
            {
                var plugin = GetPluginByAssembly(pluginServiceRegistrator.Assembly);
                if (plugin == null)
                {
                    _logger.LogError("Unable to find plugin in assembly {Assembly}", pluginServiceRegistrator.Assembly.FullName);
                    continue;
                }

                UpdatePluginSuperceedStatus(plugin);
                if (!plugin.IsEnabledAndSupported)
                {
                    continue;
                }

                try
                {
                    var instance = (IPluginServiceRegistrator?)Activator.CreateInstance(pluginServiceRegistrator);
                    instance?.RegisterServices(serviceCollection);
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    _logger.LogError(ex, "Error registering plugin services from {Assembly}.", pluginServiceRegistrator.Assembly.FullName);
                    if (ChangePluginState(plugin, PluginStatus.Malfunctioned))
                    {
                        _logger.LogInformation("Disabling plugin {Path}", plugin.Path);
                    }
                }
            }
        }