ComponentNotFound 异常 Castle.Windsor
我正在尝试开始使用 Castle.Windsor 并根据我对当前可供新手使用的示例所做的评论(http://stw.castleproject.org/Windsor.Silvertlight_Sample_App_Customer_contact_manager.ashx?Discuss=1),我想我会勇敢地面对并更新此处提供的示例 http://dotnetslackers.com/articles/designpatterns /InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx。
这是一个简单且相当直接的控制台应用程序,使用 Castle Windsor,尽管是一个过时的版本。我在 Program.cs 中的 Main 方法如下:
public static void Main()
{
IWindsorContainer container = new WindsorContainer();
container.Install(FromAssembly.This());
var retriever = container.Resolve<IHtmlTitleRetriever>();
Console.WriteLine(retriever.GetTitle(new Uri(ConfigurationManager.AppSettings["fileUri"])));
Console.Read();
container.Dispose();
}
服务和组件都在同一个文件中,即 Program.cs 如下:
public interface IHtmlTitleRetriever
{
string GetTitle(Uri file);
}
public interface IFileDownloader
{
string Download(Uri file);
}
public interface ITitleScraper
{
string Scrape(string fileContents);
}
public class HtmlTitleRetriever: IHtmlTitleRetriever
{
private readonly IFileDownloader _downloader;
private readonly ITitleScraper _scraper;
public HtmlTitleRetriever(IFileDownloader dowloader, ITitleScraper scraper)
{
_downloader = dowloader;
_scraper = scraper;
}
public string GetTitle(Uri file)
{
string fileContents = _downloader.Download(file);
return _scraper.Scrape(fileContents);
}
}
public class HttpFileDownloader : IFileDownloader
{
public string Download(Uri file)
{
return new WebClient().DownloadString(file);
}
}
public class StringParsingTitleScraper : ITitleScraper
{
public string Scrape(string fileContents)
{
string title = string.Empty;
int openingTagIndex = fileContents.IndexOf("<title>");
int closingTagIndex = fileContents.IndexOf("</title>");
if(openingTagIndex != -1 && closingTagIndex != -1)
title = fileContents.Substring(openingTagIndex, closingTagIndex - openingTagIndex).Substring(7);
return title;
}
}
它几乎是 Simone Busoli 从他的示例中获得的内容的直接副本。代码编译正常,但运行应用程序时出现以下错误:
找不到支持 WindsorSample.IHtmlTitleRetriever 服务的组件
我明白这意味着什么,但我不知道我做错了什么,组件没有加载到容器中。我正在使用 Castle.Windsor 2.5.2 和 .NET 4.0。
期待答案,
大卫
I am trying to get started with Castle.Windsor and following a comment I made on the samples currently available for newbies (http://stw.castleproject.org/Windsor.Silvertlight_Sample_App_Customer_contact_manager.ashx?Discuss=1), I thought I'd take the bull by the horns and update the example provided here http://dotnetslackers.com/articles/designpatterns/InversionOfControlAndDependencyInjectionWithCastleWindsorContainerPart1.aspx.
This is a simple and fairly straightforward console application making use of Castle Windsor, albeit an outdated version. My Main method in Program.cs is as follows:
public static void Main()
{
IWindsorContainer container = new WindsorContainer();
container.Install(FromAssembly.This());
var retriever = container.Resolve<IHtmlTitleRetriever>();
Console.WriteLine(retriever.GetTitle(new Uri(ConfigurationManager.AppSettings["fileUri"])));
Console.Read();
container.Dispose();
}
and the Service and Components, which are all in the same file i.e. Program.cs are thus:
public interface IHtmlTitleRetriever
{
string GetTitle(Uri file);
}
public interface IFileDownloader
{
string Download(Uri file);
}
public interface ITitleScraper
{
string Scrape(string fileContents);
}
public class HtmlTitleRetriever: IHtmlTitleRetriever
{
private readonly IFileDownloader _downloader;
private readonly ITitleScraper _scraper;
public HtmlTitleRetriever(IFileDownloader dowloader, ITitleScraper scraper)
{
_downloader = dowloader;
_scraper = scraper;
}
public string GetTitle(Uri file)
{
string fileContents = _downloader.Download(file);
return _scraper.Scrape(fileContents);
}
}
public class HttpFileDownloader : IFileDownloader
{
public string Download(Uri file)
{
return new WebClient().DownloadString(file);
}
}
public class StringParsingTitleScraper : ITitleScraper
{
public string Scrape(string fileContents)
{
string title = string.Empty;
int openingTagIndex = fileContents.IndexOf("<title>");
int closingTagIndex = fileContents.IndexOf("</title>");
if(openingTagIndex != -1 && closingTagIndex != -1)
title = fileContents.Substring(openingTagIndex, closingTagIndex - openingTagIndex).Substring(7);
return title;
}
}
It is pretty much a straight copy of what Simone Busoli has from his example. The code compiles fine but I get the following error when I run the application:
No component for supporting the service WindsorSample.IHtmlTitleRetriever was found
I understand what that means but I don't know what I'm doing wrong that the components are not getting loaded into the container. I'm using Castle.Windsor 2.5.2 and .NET 4.0.
Looking forward to the answers,
David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先 - 太棒了你正在更新示例。正如您所提到的,没有 Silverlight 开销的更小的示例肯定会很有用。
至于您的问题 - 异常意味着您注册的任何组件都不支持您请求的服务
IHtmlTitleRetriever
,换句话说 - 您似乎没有注册IHtmlTitleRetriever
> 完全在容器中。您的安装程序是什么样的?要检查组件是否正确注册,您可以在调用Install
后在行中放置一个断点,并查看注册了哪些组件,以及这是否是您所期望的。它对于快速诊断您遇到的问题非常有用。 (请参阅此处的文档)。如果您仍然不确定原因是什么,请确保您的安装程序类都是公共的,并将其代码发布到此处,以便我们可以尝试找出原因。
First of all - awesome you're updating the sample. As you mentioned, an even smaller sample, without Silverlight overhead will certainly be useful.
As for your question - the exception means that the service you requested,
IHtmlTitleRetriever
is not supported by any component you registered, in other words - you didn't seem to have registeredIHtmlTitleRetriever
in the container at all. How does your installer look like? To check if components get registered properly you can put a breakpoint in the line after you callInstall
and see what components were registered, and if that's what you were expecting. It's quite useful for quickly diagnosing issues like the one you're having. (see the documentation here).If you're still not sure what the reason is, make sure your installer classes are all public, and post their code here so that we can try to figure out what's the reason.
遵循 Krzysztof 的建议,以下是如何运行我的简单示例:
这会起作用。
Krzysztof,我们也尝试过这个
并适当地装饰了所有的类,但我仍然遇到与以前相同的错误。您能解释一下吗?
Following Krzysztof's advice, here is how to get my simple example going:
That will work.
Krzysztof, we also tried this
and adorned all the classes appropriately but I still got the same error as before. Can you shed some light on this please?