问题
So, I have an IConnectionFactory
interface, implemented by the NMSConnectionFactory
class with a constructor that looks like this:
public NMSConnectionFactory(string providerURI, params object[] constructorParams)
I need to create these on demand, and the providerURI
will only be known at runtime.
So I'm trying to use a delegate-based factory that creates IConnectionFactory
s for me (let's call it connectionFactoryProvider
).
After reading the Castle documentations, I've tried this: My other class's constructor depends on this factory as such:
public ActiveMqSessionPool(Func<string, IConnectionFactory> connectionFactoryProvider)
{...}
Inside this constructor, I use the provider to create IConnectionFactory
s as such:
var connectionFactory = _connectionFactoryProvider("my destination");
However, when I run this last line I get an error saying that the NMSConnectionFactory is waiting on these dependencies:
- Parameter 'providerURI' which was not provided. Did you forget to set the dependency?
- Service 'System.Object[]' which was not registered.
How can I pass the providerURI
to the NMSConnectionFactory constructor?
This is what my component registration looks like so far:
container.Register(Component.For<IConnectionFactory>()
.ImplementedBy<NMSConnectionFactory>())
.Register(Component.For<ISessionPool>()
.ImplementedBy<ActiveMqSessionPool>().LifeStyle.Transient)
.AddFacility<TypedFactoryFacility>();
Do I need to configure something else? What am I missing here?
回答1:
Your NMSConnectionFactory's constructor has the signature (string, object[]), so your factory method needs to have the same I think.
Try changing:
public ActiveMqSessionPool(Func<string, IConnectionFactory> connectionFactoryProvider)
to:
public ActiveMqSessionPool(Func<string, object[], IConnectionFactory> connectionFactoryProvider
来源:https://stackoverflow.com/questions/19096149/castle-windsor-delegate-based-factory-passing-parameters-to-the-constructor