将 Configuration Manager.GetSection 与 Configuration Manager.OpenExe 结合使用

发布于 2024-09-28 15:03:58 字数 1542 浏览 5 评论 0原文

我有一个通过 MEF 使用 DLL 的可执行文件。 成功加载了每个 DLL 的配置文件的 appsettings 键,

    var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
        return appConfig.AppSettings.Settings["Version"].Value; 

我使用“现在我想要这样做”

以便 DLL 允许在 DLL 的配置文件中使用临时项。所以我将其添加到配置文件中

    <configSections>
            <section name="AdHocRules" type="BusinessRules.AdHocConfiguration, BusinessRules" />
    </configSections>   
                <AdHocRules BaseRuleNumber="ConfigEx1" RuleName="Config Example" EffectiveDate="5/1/2010" Value="Example" IsValid="true"/>

并且创建了一个类来读取上面的内容。当我在不消耗 DLL 的测试控制台应用程序中运行它时 - 所以所有内容都编译在一起并且单个应用程序配置一切正常

但是 - 我想使用 DLL 的配置文件,但我不断收到错误

无法转换类型的对象 '系统.配置.DefaultSection' 键入 'BusinessRules.AdHocConfiguration

这不起作用; - 它抛出了上面的内容

var cm = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
AdHocConfiguration adhoc = (AdHocConfiguration)cm.GetSection("AdHocRules");

这是代码 - adhoc 为空,因为它没有从正确的配置文件加载

    AdHocConfiguration adhoc = (AdHocConfiguration)ConfigurationManager.GetSection("AdHocRules");


            BusinessRules.Rule r = new BusinessRules.Rule();
            r.BaseRuleNumber = adhoc.baserulenumber;
            r.RuleName = adhoc.rulename;
            r.EffectiveDate = adhoc.effectivedate;
            r.Value = adhoc.value;

有什么想法吗?

I have an executable that consumes DLL’s through MEF. I am successfully loading each DLL’s config file's appsettings keys using

    var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
        return appConfig.AppSettings.Settings["Version"].Value; 

Now I want to make it so the DLL allows for adhoc items in the DLL’s config file.

So I added this to the config file

    <configSections>
            <section name="AdHocRules" type="BusinessRules.AdHocConfiguration, BusinessRules" />
    </configSections>   
                <AdHocRules BaseRuleNumber="ConfigEx1" RuleName="Config Example" EffectiveDate="5/1/2010" Value="Example" IsValid="true"/>

And I created a class to read the above. And when I run this in a test console app that is not consuming the DLL – so everything is complied together and a single app config everything works fine

BUT – I want to use the DLL’s config file and I keep getting an error

Unable to cast object of type
'System.Configuration.DefaultSection'
to type
'BusinessRules.AdHocConfiguration

This is not working; - it's throwing the above

var cm = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
AdHocConfiguration adhoc = (AdHocConfiguration)cm.GetSection("AdHocRules");

And this is code – adhoc is null because it is not loading from the correct config file

    AdHocConfiguration adhoc = (AdHocConfiguration)ConfigurationManager.GetSection("AdHocRules");


            BusinessRules.Rule r = new BusinessRules.Rule();
            r.BaseRuleNumber = adhoc.baserulenumber;
            r.RuleName = adhoc.rulename;
            r.EffectiveDate = adhoc.effectivedate;
            r.Value = adhoc.value;

Any ideas?

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

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

发布评论

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

评论(1

峩卟喜欢 2024-10-05 15:03:58

为了使用 OpenExeConfiguration 方法,其他 DLL 的配置文件需要驻留在与可执行文件相同的目录中,如 MSDN 参考

您可能需要一个构建后事件来移动配置文件,但它确实有效。

另外,您可能想使用 Assembly.GetAssembly(MEF 加载的某种类型).Location;而不是 Assembly.GetExecutingAssembly().Location,具体取决于您如何使用它。

我有一个示例项目,我使用 MEF 加载部件,并读取它们的配置文件。

如果您仍然遇到问题,请告诉我

In order to use the OpenExeConfiguration method, the configuration files for the other DLLs need to reside in the same directory as the executable file as they mention in the MSDN Reference.

You might need a post-build event to move the config files, but it does work.

Also you might want to use Assembly.GetAssembly(some type loaded by MEF).Location; rather than Assembly.GetExecutingAssembly().Location, depending on how you are using it.

I have a sample project where I load parts using MEF, and read their config files.

Let me know if you still have trouble

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