Question

I have successfully setup castle windsor using an xml configuration file and everything works fine. The only problem is that on a method I need Windsor to pass an existing instance to the constructor so I used

container.Kernel.AddComponentInstance<IMyClass>(MyClassInstance);

before the Resolve method but that does not work because Windsor creates an new instance of IMyClass based on the xml config file. That behavior if fine on every other place except this particular method that I need to pass the existing instance. How can I solve that problem. Tried to find documentation on the web but no luck until now :(.

Was it helpful?

Solution

You can do this:

var instance = new MyClass();
container.Register(Component.For<IMyClass>().Instance(instance));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top