【问题标题】:Referenced assembly can not read app.config引用的程序集无法读取 app.config
【发布时间】:2012-07-16 01:29:59
【问题描述】:

我的单元测试有问题。问题是:我正在使用 Sitecore。我问的另一个问题是:Unit Testing Sitecore LicenseManager

我的问题是我得到了异常:

Test method foo.Bar.Framework.Tests.Core.UnitTest1.TestMethod1 threw exception: 
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'Sitecore.Services.Heartbeat' threw an exception. ---> System.InvalidOperationException: Could not read Sitecore configuration.

我使用 dotPeek 查看了代码,函数 Sitecorre.Configuration.Settings.GetSetting("HeartbeatInterval") 引发异常。因为它无法读取配置。 当我将相同的代码添加到我的单元测试 .cs 文件并调试测试时,它可以读取配置。 该设置也可在 app.config 中使用

看起来引用的程序集无法读取配置,而测试本身可以。 我该如何解决这个问题?

编辑

Settings.GetSetting() 函数最终使用 Sitecore.Configuration.ConfigReader.GetConfigNode()。这个函数最终会用到这个函数:

private static ConfigReader GetReader()
{
    return ConfigurationManager.GetSection("sitecore") as ConfigReader;
}

所以... 为什么引用的dll无法通过ConfigurationManager读取app.config?

【问题讨论】:

  • 你的app.config是在测试项目还是在其他项目?
  • 它在测试项目中。构建操作:无。复制到输出目录:不要复制。显然,这些程序集位于 References 文件夹中
  • 它是否适用于常规项目?
  • 是的,我在 web.config 中设置了相同的设置,并且代码项目运行良好
  • 也许异常意味着它被发现但由于语法错误而不可读。尝试删除该部分并重建。你有同样的例外吗?

标签: c# unit-testing sitecore appsettings


【解决方案1】:

原来是 *.testsettings。 当我在我的电脑上运行它时,它可以工作,但在构建服务器上却没有。结果我只需要提交 *.testsettings 并包含 /testsettings 参数

【讨论】:

    猜你喜欢
    • 2013-06-21
    • 2015-10-15
    • 2018-12-27
    • 2011-04-03
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 2013-04-25
    相关资源
    最近更新 更多