R – IWindsorContainer as a parameter to a class

castle-windsorinversion-of-controlnet

I have a class that I want to have access to my IOC container (Windsor), however I don't want to keep a static IWindsorContainer property hanging around – I would prefer to have the container inject itself into any classes that require an IWindsorContainer as a constructor dependency.

I've pulled this off with Unity, but when I try the same thing with the Windsor container it tells me that IWindsorContainer is not registered with the container.

I don't think I can just register IWindsorContainer => WindsorContainer, because that will cause the container to create a new (or different) instance of itself to pass to my class, and that instance won't have all my other types registered with it. I also don't see a way to construct the container, register all the types in it, and then register that instance of itself against IWindsorContainer – all of the registration methods only take types for service and implementation – never an actual concrete instance.

Best Answer

Generally you don't want to inject the container into your application components.

See these questions (this question is almost a duplicate of them):

BTW: you get IKernel injection for free, and you can register IWindsorContainer:

container.Register(Component.For<IWindsorContainer>().Instance(container));