问题
Trying to make use of the Castle Windsor IoC. I have a very simple application. My interfaces exist in Test.Services namespace. I get the following exception when compiling:
"The type name Test.Services.IParse, Test.Services could not be located"
This is my app.config:
<configuration>
<configSections>
<section name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,
Castle.Windsor" />
</configSections>
<castle>
<components>
<component id="HtmlTitleRetriever"
type="Test.HTMLTitleRetriever, Test">
</component>
<component id="FileParser"
service="Test.Services.IParse, Test.Services"
type="Test.FileParser,
Test">
</component>
<component id="FileDownloader"
service="Test.Services.IDownload, Test.Services"
type="Test.FileDownloader, Test">
</component>
</components>
</castle>
Can someone tell me what I'm missing?
Thanks
-Nick
回答1:
Stupid question, but are you certain that the assembly containing the class you register gets actually copied to the output directory?
回答2:
IWindsorContainer container = new WindsorContainer();
container.AddComponent("FileDownlader", typeof(IDownload),
typeof(FileDownloader));
container.AddComponent("FileParser", typeof(IParse),
typeof(FileParser));
container.AddComponent("HTMLTitleRetriever", typeof(HTMLTitleRetriever));
HTMLTitleRetriever retriever = container.Resolve<HTMLTitleRetriever>();
Doing the following in code works. I would like to use a Config file though so changes do not need to be recompiled each time.
回答3:
Almost Nick! You need to pass the xml configuration into the constructor of WindsorContainer:
IWindsorContainer container;
container =
new WindsorContainer(
new XmlInterpreter(
new ConfigResource("castle")
));
I ran into this "Got ya" as well when I first started playing with Castle Windsor. It does not know to look into the configuration section.
Tip: go get the latest from their trunk. There's a new method called Register that is really useful for mass-registering multiple classes on the same interface. I.e., if you are developing with ASP.NET MVC, and you have a bunch of IController, you can use Castle Windsor to auto-register them for you. So, you do not have to specify them in the configuration file!
public class WindsorControllerFactory : IControllerFactor
{
IWindsorContainer container;
public WindsorControllerFactory()
{
container =
new WindsorContainer(
new XmlInterpreter(
new ConfigResource("castleWindsor")
));
container.Register(
AllTypes.Of<IController>()
.FromAssembly(Assembly.GetExecutingAssembly())
.Configure(component => component.LifeStyle.Transient
.Named(component.Implementation.Name)
));
}
}
回答4:
Is the IParse interface in the Test.Services assembly (Test.Services.dll) ?
The format used to define type names in the config is the same you can get from the AssemblyQualifiedName property of the type. In a nutshell, it's:
Namespace.Type, Assembly.
For generic types, see this.
回答5:
I had the same issue and Eric's solution worked to resolve the problem. I Created a "Domain Models" Project and then later renamed it to "DomainModel" I though I changed all the references but then looking in the bin folder the assembly was still called "Domain Models" thats why castle windsor could not find it.
来源:https://stackoverflow.com/questions/624310/castle-windsor-cannot-find-my-service-type