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");
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
: ( )/ - , ? : , ( )?