Autofac XML Configuration & Properties Override or Provide if not present?
I've got an application using Autofac at the moment. I've structured my autofac to register from the config file after the modules are loaded in. (For an xml-based override of the default behaviour).
Right now I've got a class with a bunch of basic environmental settings (a slew of public auto-properties really). This class tries to guess the default configuration based on some settings, but the idea is that each environment the app runs under would override these settings in the xml config.
Here's my problem:
Earlier in the development of this system, things were peachy. Now it seems to be completely ignoring the input of the XML Configs. I am unsure what changed that caused this to get broken, so I'm hoping someone can point out something obvious I am doing wrong.
I have verified the config file is getting read/parsed by autofac, just not applied.
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 Building Code
var builder = new ContainerBuilder();
// Register Identifier so it's 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 is 2.3.2.632
For .NET 3.5
The problem is in your <files>
section. Since this is handled by FileElementCollection
, a configuration class inheriting the NamedConfigurationElementCollection
class and using the name
property as its key value, multiple entries must have unique name
values.
Since you are referring to the same name
twice, the first entry referring to the autofac-common
section is effectively overwritten by the second entry. Thus, the service will never be registered, only the CachingModule
.
I cannot say whether this is new behavior on Autofac's part or not, as far as I can tell from the source, the FileElementCollection
class was introduced in 2.0 and haven't changed logic since then. Could it be that you have referenced different .config files earlier?
精彩评论