问题
抓取 时_mailSettings.Smtp.Network.Host
,即使它在应用程序配置文件中,它也会返回null
.
代码
首先,因为项目只是一个Class Library
,我有以下构建后事件来构建正确命名的配置文件:
copy "$(ProjectDir)App.config" "$(TargetDir)ApplicationSupport.Email.dll.config"
接下来我有一个旨在加载配置并使其可用的类:
public class Startup
{
SmtpSection _smtpSection = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
public string SmtpServer
{
get
{
if (_smtpSection == null) { return string.Empty; }
return _smtpSection.Network.Host;
}
}
...
}
我正在尝试对其进行单元测试:
[TestMethod]
public void TestStartupSettings()
{
var sut = new ApplicationSupport.Email.Startup();
Assert.AreEqual("test.mail.com", sut.SmtpServer);
}
我认为我应该获取数据,因为我已经在应用程序配置文件中配置了这个:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<mailSettings>
<smtp from="somebody@test.com">
<network host="test.mail.com" defaultCredentials="true" port="25"/>
</smtp>
</mailSettings>
</system.net>
</configuration>