Exemplo n.º 1
0
        public bool Run(string crawlPath, CancellationToken?cancellationToken = null)
        {
            try
            {
                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

                crawlPath = FindCrawlDLL <ICrawl>(crawlPath);

                var loader = PluginLoader.CreateFromAssemblyFile(
                    crawlPath,
                    sharedTypes: new[] { typeof(ICrawl) });
                var pluginType = loader
                                 .LoadDefaultAssembly()
                                 .GetTypes()
                                 .Where(t => typeof(ICrawl).IsAssignableFrom(t) && !t.IsAbstract)
                                 .FirstOrDefault();

                // This assumes the implementation of IPlugin has a parameterless constructor
                ICrawl plugin = (ICrawl)Activator.CreateInstance(pluginType);

                bool result = plugin.ScrapeData(cancellationToken);
                return(result);
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, ex, "Plugin run failed");
                return(false);
            }
        }
Exemplo n.º 2
0
        public void test()
        {
            try
            {
                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);

                var loaders = new List <PluginLoader>();

                // create plugin loaders
                var pluginsDir = Path.Combine(Directory.GetCurrentDirectory(), "plugins");
                foreach (var dir in DirSearch(pluginsDir))
                {
                    var pluginDll = Path.GetFileName(dir);

                    var loader = PluginLoader.CreateFromAssemblyFile(
                        dir,
                        sharedTypes: new[] { typeof(ICrawl) });
                    loaders.Add(loader);
                }

                // Create an instance of plugin types
                foreach (var loader in loaders)
                {
                    foreach (var pluginType in loader
                             .LoadDefaultAssembly()
                             .GetTypes()
                             .Where(t => typeof(ICrawl).IsAssignableFrom(t) && !t.IsAbstract))
                    {
                        // This assumes the implementation of IPlugin has a parameterless constructor
                        ICrawl plugin = (ICrawl)Activator.CreateInstance(pluginType);

                        Console.WriteLine($"Created plugin instance.");

                        plugin.ScrapeData();
                    }
                }
            }
            catch (Exception e)
            {
            }
        }
Exemplo n.º 3
0
        public async Task <bool> Run(PluginRunningParameters pluginRunningParameters, CancellationToken?cancellationToken)
        {
            try
            {
                ICrawl crawlPlugin = CreateInstanceOfPlugin <ICrawl>(pluginRunningParameters.CrawlerPluginPath);

                crawlPlugin.ScrapeData(cancellationToken);
                bool result = false;
                if (pluginRunningParameters.IsProcessorAssigned())
                {
                    var      productsData  = crawlPlugin.GetData();
                    IProcess processPlugin = CreateInstanceOfPlugin <IProcess>(pluginRunningParameters.ProcessorPluginPath);
                    result = await processPlugin.ProcessData(pluginRunningParameters.MerchantName, productsData, pluginRunningParameters.ProcessorSaveAction);
                }

                return(result);
            }
            catch (Exception ex)
            {
                _logger.Log(LogLevel.Error, ex, "Plugin run failed");
                return(false);
            }
        }