我无法从我的Windows服务的配置文件(MyService.exe.config
)中读取appSettings
。请注意,服务已成功安装。如何在启动Windows服务时阅读Windows服务配置?
[PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
public class MyService : ServiceBase
{
public MyService()
{
InitializeComponent();
ServiceName = ConfigurationManager.AppSettings.Get("ServiceName");
}
private void InitializeComponent()
{
try
{
AutoLog = true;
CanStop = true;
}
catch (Exception e)
{
// Log error
}
}
static void Main()
{
MyService myService = new MyService();
Run(myService);
}
protected override void OnStart(string[] args)
{
// Code to do necessary things on start
}
}
在事件查看器的例外是System.Configuration.ConfigurationErrorsException
这是正确的位置读取Windows服务的配置?ConfigurationManager.AppSettings
总是返回null
。
我有一个现有的服务,这应该工作:ServiceName = ConfigurationManager.AppSettings [“ServiceName”];我假设你已经检查过配置文件安装在服务目录中了吗? – 2011-04-08 11:08:10
由于问题出在配置文件中,所以代码完美地工作。 'appSettings'部分出错了。见ChrisDickson的回答。 – Learner 2011-04-08 12:32:11