Castle Windsor delegate-based factory: passing parameters to the constructor

笑着哭i 提交于 2019-12-11 05:29:39

问题


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 IConnectionFactorys 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 IConnectionFactorys 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!