C# NCrawler中的自定义机器人过滤

C# NCrawler中的自定义机器人过滤,c#,web-crawler,C#,Web Crawler,在Ncrawler中扩展接口IRobot,实现机器人过滤器的正确方法是什么 在我找到的几个文档中,说了但没有说如何做。此外,我是C#的新手,所以我不懂一些代码 特别是示例中的以下部分,其中似乎很容易引入新的规则类,但不引入新的robot过滤器: // Register new implementation for ICrawlerRules using our custom class CustomCrawlerRules defined below NCrawlerModule.Registe

在Ncrawler中扩展接口
IRobot
,实现机器人过滤器的正确方法是什么

在我找到的几个文档中,说了但没有说如何做。此外,我是C#的新手,所以我不懂一些代码

特别是示例中的以下部分,其中似乎很容易引入新的规则类,但不引入新的robot过滤器:

// Register new implementation for ICrawlerRules using our custom class CustomCrawlerRules defined below
NCrawlerModule.Register(builder =>
        builder.Register((c, p) =>
            {
                NCrawlerModule.Setup(); // Return to standard setup
                return new CustomCrawlerRules(p.TypedAs<Crawler>(), c.Resolve<IRobot>(p), p.TypedAs<Uri>(),
                p.TypedAs<ICrawlerHistory>());
            }).
        As<ICrawlerRules>().
        InstancePerDependency());
//使用下面定义的自定义类CustomCrawlerRules注册ICrawlerRules的新实现
NCrawlerModule.Register(生成器=>
建造商注册((c,p)=>
{
NCrawlerModule.Setup();//返回标准设置
返回新的customcrawlerules(p.TypedAs()、c.Resolve(p)、p.TypedAs(),
p、 TypedAs());
}).
As()。
InstancePerDependence());

RobotService
类在某个地方“注册”,但在
CustomCrawlerRules
中的任何地方都没有设置。可以找到所有代码。

尝试在NCrawlerModule注册表中注册您自己的CustomRobotService

代码应该是这样的:

builder.Register((c, p) => new CustomRobotService(p.TypedAs<Uri>(), c.Resolve<IWebDownloader>())).As<IRobot>().InstancePerDependency();
builder.Register((c,p)=>newcustomrobotservice(p.TypedAs(),c.Resolve()).As().InstancePerDependency();

谢谢你的工作!!我用与自定义爬虫相同的方式添加了它,非常简单,但我没有看到它。问:如果我想写具体的解决方案代码,我应该在哪里插入它?模块的注册应该只完成一次,它将接口(抽象)连接到它们的实现。如果您不提供自己的实现,NCrawler将使用默认实现。通常,解决方案的依赖项注入注册位于应用程序的引导程序中,该引导程序在任何其他应用程序代码之前只运行一次。但是有些用例可能更复杂,对于这些,我建议对如何使用Autofac进行一些研究,因为这是NCrawler使用的DI容器。