Are Autofac's XML configuration and properties overridden or provided, if not?

I have an application using Autofac at the moment. I designed autorun to register from the configuration file after loading the modules. (To override xml-based default behavior).

Now I have a class with a lot of basic environment settings (in fact, a lot of public auto-properties). This class tries to guess the default configuration based on some settings, but the idea is that every environment in which the application runs will override these parameters in the xml configuration.

Here is my problem:

Previously, in the development of this system, everything was Persian. Now it seems to completely ignore XML Configs input. I'm not sure what has changed, what caused it to break, so I hope that someone can point out something obvious, I'm doing wrong.

I checked that the configuration file is read / parsed using autofac, just not applicable.

web.config:

<autofac> <files> <file name="Config/Environment.config" section="autofac-common" /> <file name="Config/Environment.config" section="autofac-public" /> </files> </autofac> 

Environment.config:

 <configuration> <configSections> <section name="autofac-common" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/> <section name="autofac-public" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/> </configSections> <!-- Common --> <autofac-common> <components> <component type="MyApp.Web.Configuration.ServerConfiguration, MyApp.Web" service="MyApp.Web.Configuration.IServerConfiguration, MyApp.Web"> <properties> <property name="Host" value="beta.mysite.com" /> <property name="MediaHost" value="beta.media.mysite.com" /> </properties> </component> </components> </autofac-common> <!-- Web Site --> <autofac-public> <modules> <module type="MyApp.Configuration.CachingModule, MyApp"> <properties> <property name="Disable" value="true" /> </properties> </module> </modules> </autofac-public> </configuration> 

Container design code

  var builder = new ContainerBuilder(); // Register Identifier so it available to modules builder.RegisterType<ServerIdentifier>() .As<IServerIdentifier>() .SingleInstance(); var container = builder.Build(); builder = new ContainerBuilder(); builder.RegisterModule(new CachingModule() { Disable = true }); builder.RegisterModule(new LoggerModule()); builder.RegisterModule(new InventoryModule()); builder.RegisterModule(new CatalogModule()); builder.RegisterModule(new WebModule(container)); // Override with settings from XML builder.RegisterModule(new ConfigurationSettingsReader("autofac")); builder.Update(container); 

Autofac Version 2.3.2.632 For .NET 3.5

+4
source share
1 answer

The problem is in your <files> section. Since this is handled by FileElementCollection , a configuration class that inherits from the NamedConfigurationElementCollection class and uses the name property as its key value, several records must have unique name values.

Since you are referring to the same name twice, the first entry related to the autofac-common section is actually overwritten by the second entry. Thus, the service will never be registered, only CachingModule .

I can’t say whether this is a new behavior in the Autofac part or not, as far as I can tell from the source, the FileElementCollection class was introduced in 2.0 and has not changed the logic since. Maybe you previously referenced different .config files?

+3
source

All Articles