【问题标题】:Loading Custom Configuration Section from DLL Reference从 DLL 参考加载自定义配置部分
【发布时间】:2016-08-03 00:40:53
【问题描述】:

在我们的项目中,我们定义了一个自定义配置部分,在项目中被引用时可以正常工作。现在我们尝试从作为参考添加的 dll 中引用相同的配置部分。通过这个 dll 中的代码,我们可以毫无问题地访问 ConfigurationManager.AppSettings,但在访问配置条目时会出错。

Web.config 代码

<section name="mailManager" type="FullNamespace, NameSpace" />

<mailManager prop1="propVal1">
    <prop2 key1="keyVal1" key2="keyVal2" key3="keyVal3" />
    <prop3 key1="keyVal1" key2="keyVal2" />
</mailManager>

在引用它的 dll 中尝试获取配置部分时会引发以下错误。此部分存在于 dll 解决方案和主解决方案代码库中。

var mailManagerConfigSection = ConfigurationManager.GetSection("mailManager") as EmailManagerConfigSection;

我们得到的错误是 error CS0433: 类型 'EmailManagerConfigSection' 同时存在于 'namespace1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 和 'namespace2, Version=1.0.0.0, Culture =中性,PublicKeyToken=null'。

dll 引用存在于运行主代码的同一 bin 目录中。无论如何让dll引用具有值的主EmailManagerConfigSection,而不是dll的本地内容为null?我们不想引入任何 dll.config 文件。

【问题讨论】:

    标签: c# web-config custom-configuration


    【解决方案1】:

    必须从可执行文件而不是 DLL 定义、加载和引用配置部分。

    您可以将 dll 项目中配置文件的源代码保留在您的解决方案空间中,但无论 dll 的编译将配置文件放在何处,主机可执行文件的 app.config (引用 dll 的文件)必须在其 app.config [ApplicationName.Exe.config] 中声明并指定该文件,只需将配置的适当 &lt;ConfigSections&gt; &lt;section&gt; 元素和实际部分元素添加到主机可执行文件 app.config 中。 /p>

    【讨论】:

    • 感谢 Charles 当我看到 ConfigurationManager.AppSettings["asVal"] 获得条目时,所以我希望我能做类似的事情,但我能理解为什么这是不可能的。感谢您的时间。
    猜你喜欢
    • 2012-04-17
    • 2016-07-19
    • 2011-03-06
    • 2013-09-07
    • 2010-10-25
    • 2010-11-12
    • 2013-09-17
    • 2011-07-04
    相关资源
    最近更新 更多