【问题标题】:Do app.config files support being called from higher layers?app.config 文件是否支持从更高层调用?
【发布时间】:2012-06-15 14:56:33
【问题描述】:

我为第 3 方应用程序编写扩展程序。该应用程序调用我的类库。当从另一个程序调用时,我无法让我的程序集从他们的 app.config 数据中读取。下面通过单元测试重现了该问题。

ConsoleApplication.exe 中的 App.config

<appSettings>
    <add key="testKey" value="testvalue" />
</appSettings>

    static void Main(string[] args)
    {
        TestClass c = new TestClass();
        c.Run();
    }

    //TestClass.Run()
    public void Run()
    {
        var readAppConfig = ConfigurationManager.AppSettings[0];
        MessageBox.Show("App config: " + readAppConfig);
    }

单元测试代码,MSTEST

    [TestMethod]
    public void TestAppConfig()
    {
        TestClass c = new TestClass();
        c.Run();//fails here
    }

Main() 有效,TestAppConfig() 失败并出现 ArgumentOutOfRangeException

索引超出范围。必须是非负数且小于 集合。

App.config 从外部程序集调用时是否停止工作?

【问题讨论】:

标签: c# .net


【解决方案1】:

应用设置必须位于最高级别的组件中(在这种情况下,您的 MSTEST app.config 需要保存设置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多