Autofac XML 配置和如果属性不存在,则覆盖或提供?

发布于 2024-10-05 22:51:31 字数 2669 浏览 0 评论 0原文

我现在有一个使用 Autofac 的应用程序。我已经将 autofac 构造为在加载模块之后从配置文件进行注册。(对于默认行为的基于 xml 的覆盖)。

现在我有一个包含大量基本环境设置的类(实际上是大量公共自动属性)。此类尝试根据某些设置猜测默认配置,但其想法是应用程序运行的每个环境都会覆盖 xml 配置中的这些设置。

这是我的问题:

在这个系统的开发早期,一切都很顺利。 现在它似乎完全忽略了 XML 配置的输入。我不确定是什么变化导致了这个问题,所以我希望有人能指出我做错的事情。

我已经验证了配置文件正在被 autofac 读取/解析,只是没有应用。

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>

容器构建代码

    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 版本为 2.3.2.632 对于 .NET 3.5

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挥剑断情 2024-10-12 22:51:31

问题出在您的 部分。由于这是由 FileElementCollection 处理的,配置类继承 NamedConfigurationElementCollection 类并使用 name 属性作为其键值,多个条目 必须具有唯一 name 值。

由于您两次引用同一个 name,因此引用 autofac-common 部分的第一个条目实际上被第二个条目覆盖。因此,服务永远不会被注册,只会注册CachingModule

我不能说这是否是 Autofac 的新行为,据我从源代码中可以看出,FileElementCollection 类是在 2.0 中引入的,从那时起就没有改变逻辑。是否您之前引用了不同的 .config 文件?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文