Register Multiple Components for Single Interface

2020-07-27 04:56发布

问题:

I am trying to register multiple NHibernate ISessions (multiple databases) by using the code below. I am getting "There is a component already registered for the given key Castle.MicroKernel.Registration.GenericFactory`1[[NHibernate.ISession, NHibernate, Version=2.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4]]" as the error message when the container is trying to be built.

container.Kernel.Register(
    Component.For<ISession>().LifeStyle.Transient
        .UsingFactoryMethod(() => sessionFactoryOne.OpenSession()
        ).Named("ISession+sessionOne"));

container.Kernel.Register(
    Component.For<ISession>().LifeStyle.Transient
        .UsingFactoryMethod(() => sessionFactoryTwo.OpenSession())
         .Named("ISession+sessionTwo"));

回答1:

This was fixed a couple of months ago. Please get a recent build from the buildserver.