Custom config injection - New for Castle Windsor / IoC

Hi, I am new to Castle Windsor and struggling to understand some of the fundamental principles, so I decided that I posed a question and did not go through the code in the hope of solving my problem earlier.

I have a web service that should retrieve information from the web.config configuration file. This information is in a custom configuration section, and I wonder how I can get this information in the corresponding class. I do not want to bind this class to the configuration file, since I can host it through IIS or the Windows user service. My first attempt was to do something like this:

iocCon.Register(Component.For<ErrorMessagesSection>().LifeStyle.Singleton.Instance(FindConfigSection<ErrorMessagesSection>())); 

private T FindConfigSection<T>() where T : ConfigurationSection
    {
        System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/web.config");//TODO: remove this hard coding to iis hosting  .OpenExeConfiguration(ConfigurationUserLevel.None);
        ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;

        foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
            foreach (ConfigurationSection configurationSection in sectionGroup.Sections)
                if (configurationSection.GetType() == typeof(T))
                    return (T) configurationSection;
        return null;
    }

, , factory. , , , ( , ...) , . :

UGLY_HACK = new ConfigFileErrorMessageManager(eMessages);
iocContationer.Register(Component.For<IErrorMessageManager>().ImplementedBy<ConfigFileErrorMessageManager>().LifeStyle.Singleton.Instance(UGLY_HACK));

, :

[ComponentRegistrationException: This component has already been assigned implementation xxx.ConfigFileErrorMessageManager]
Castle.MicroKernel.Registration.ComponentRegistration`1.ImplementedBy(Type type,     IGenericImplementationMatchingStrategy genericImplementationMatchingStrategy) +310

Castle.MicroKernel.Registration.ComponentRegistration`1.Instance(TService instance) +44

: ( )/ - , ? : , ( )?

+3
1

, , , ConfigurationManager, Windsor.

IConfigurationManager WebConfigurationManager, .

container.Register(
    Component.For<IConfigurationManager>()
             .Instance(new WebConfigurationManagerAdapter()));

IConfigurationManager , .

, factory Windsor IConfigurationManager, , . -

container.Register(
    Component.For<ErrorMessagesSection>()
             .UsingFactoryMethod(kernel => 
                 kernel.Resolve<IConfigurationManager>()
                       .GetSection<ErrorMessagesSection>("errorSectionName")));
+4

All Articles