gpt4 book ai didi

C#:System.Configuration.Configuration 在寻找什么?

转载 作者:行者123 更新时间:2023-11-30 15:03:33 25 4
gpt4 key购买 nike

我有以下测试方法:

[TestMethod]
public void TestHarvestMethod()
{
HarvestTargetTimeRangeUTC time = new HarvestTargetTimeRangeUTC();
time.StartTimeUTC = new DateTime(2008, 01, 01, 00, 00, 00, DateTimeKind.Utc);
time.EndTimeUTC = DateTime.UtcNow;
XElement lIntelexReport = XElement.Parse(rawXml);
Harvester target = new Harvester();
target.ConfigureHarvester((System.Configuration.Configuration)null);
var res = target.Harvest(time);
Console.WriteLine(res);
}

与此方法结合使用:

public void ConfigureHarvester(System.Configuration.Configuration configuration)
{
reportId = Int32.Parse(configuration.AppSettings.Settings["IncidentReport"].Value);
}

测试这个方法:

public XElement Harvest(HarvestTargetTimeRangeUTC ranges)
{
XElement lIntelexReport = IntelexServiceCall();
return XMLConversion(QueryData(ranges, lIntelexReport));
}

问题是我收到 Null Exception 错误,指出“对象引用未设置到对象的实例”。在这条线上:

reportId = Int32.Parse(configuration.AppSettings.Settings["IncidentReport"].Value);

我几乎肯定是由这里的空值引起的:

target.ConfigureHarvester((System.Configuration.Configuration)null);

上面一行的System.Configuration是本店常用的,但通常是这样的方法:

public void ConfigureHarvester(System.Configuration.Configuration configuration)
{
context = new PlannedOutageFactorDataContext();
}

所以我的 reportid 字段显然在寻找 null 值以外的东西,问题是我不知道它在寻找什么。我阅读了 System.Configuration 的 MSDN,但它确实没有帮助。如果有人能指出我正确的方向,我将不胜感激。

最佳答案

它正在寻找您将 web.config 或 app.config 值的副本传递给它,以便它可以从中提取所需的信息(保存在 AppSettings 部分)

例如

<configuration>
<appSettings>
<add key="IncidentReport" value="1" />
</appSettings>
</configuration>

如果您测试网页,很多时候您需要设置网络服务的位置,以便它可以从您的网站获取 web.config 的副本。

如果您正在开发控制台/桌面应用程序,请确保您有一个 app.config 文件

或者,您可以使用

手动传递它
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);

关于C#:System.Configuration.Configuration 在寻找什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11402282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com