2012-12-27 51 views
0

哪種方法可以實現機器人篩選器,在Ncrawler中擴展接口IRobotNCrawler中的自定義機器人篩選

在我發現的幾個文檔中,請說it is possible,但不說怎麼做。此外,我是C#中的新手,所以我不瞭解一些代碼。

特地在實例中發現的以下部分,在那裏似乎很容易引入新的規則類,但不是一個新的機器人過濾器:

// 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()); 

RobotService類是「登記」在某個地方,但在沒有設置好的CustomCrawlerRules內的任何地方。所有的代碼可以找到here

回答

1

嘗試註冊自己的CustomRobotServiceNCrawlerModule註冊表中。

的代碼看起來應該是這樣的:

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

謝謝你的工作!我用'CustomCrawler'的方式添加它,非常簡單,但我沒有看到它。 問題:如果我想編寫具體的解決方案代碼,我希望在哪裏插入它? – gpupu

+0

模塊的註冊應該只做一次,它將接口(抽象)連接到它們的實現。如果你沒有提供你自己的實現,NCrawler使用默認實現。通常,解決方案的依賴注入註冊位於應用程序的引導程序中,該應用程序在任何其他應用程序代碼之前只運行一次。但是一些使用案例可能會更復雜,因此我建議對如何使用Autofac進行一些研究,因爲那是NCrawler使用的DI容器。 –

+0

謝謝tomp!現在一切都更加清晰 – gpupu