public async Task Initialize()
        {
            if (!string.IsNullOrWhiteSpace(_assemblyPath) && _assembly == null)
            {
                if (!File.Exists(_assemblyPath))
                {
                    throw new ArgumentException($"Assembly in path {_assemblyPath} does not exist.");
                }

                _pluginAssemblyLoadContext = new PluginAssemblyLoadContext(_assemblyPath, _options.PluginLoadContextOptions);
                _assembly = _pluginAssemblyLoadContext.Load();
            }

            _plugins = new List <TypePluginCatalog>();

            var finder = new TypeFinder();

            foreach (var typeFinderCriteria in _options.TypeFinderCriterias)
            {
                var pluginTypes = finder.Find(typeFinderCriteria.Value, _assembly, _pluginAssemblyLoadContext);

                foreach (var type in pluginTypes)
                {
                    var typePluginCatalog = new TypePluginCatalog(type, new TypePluginCatalogOptions()
                    {
                        PluginNameOptions = _options.PluginNameOptions
                    });
                    await typePluginCatalog.Initialize();

                    _plugins.Add(typePluginCatalog);
                }
            }

            IsInitialized = true;
        }
Exemplo n.º 2
0
        public async Task Initialize()
        {
            if (!string.IsNullOrWhiteSpace(_assemblyPath) && _assembly == null)
            {
                if (!File.Exists(_assemblyPath))
                {
                    throw new ArgumentException($"Assembly in path {_assemblyPath} does not exist.");
                }
            }

            if (_assembly == null && File.Exists(_assemblyPath) || File.Exists(_assemblyPath) && _pluginAssemblyLoadContext == null)
            {
                _pluginAssemblyLoadContext = new PluginAssemblyLoadContext(_assemblyPath, _options.PluginLoadContextOptions);
                _assembly = _pluginAssemblyLoadContext.Load();
            }

            _plugins = new List <TypePluginCatalog>();

            var finder = new TypeFinder();

            var handledPluginTypes = new List <Type>();

            foreach (var typeFinderCriteria in _options.TypeFinderOptions.TypeFinderCriterias)
            {
                var pluginTypes = finder.Find(typeFinderCriteria, _assembly, _pluginAssemblyLoadContext);

                foreach (var type in pluginTypes)
                {
                    if (handledPluginTypes.Contains(type))
                    {
                        // Make sure to create only a single type plugin catalog for each plugin type.
                        // The type catalog will add all the matching tags
                        continue;
                    }

                    var typePluginCatalog = new TypePluginCatalog(type,
                                                                  new TypePluginCatalogOptions()
                    {
                        PluginNameOptions  = _options.PluginNameOptions,
                        TypeFindingContext = _pluginAssemblyLoadContext,
                        TypeFinderOptions  = _options.TypeFinderOptions
                    });

                    await typePluginCatalog.Initialize();

                    _plugins.Add(typePluginCatalog);

                    handledPluginTypes.Add(type);
                }
            }

            IsInitialized = true;
        }