我无法从我的 Windows 服务的配置文件 (MyService.exe.config
) 中读取 appSettings
。请注意服务已成功安装。
[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
。
最佳答案
异常说明你的配置文件有问题。仔细检查。异常或其内部异常中应该有更多信息,这将为您提供更准确的错误指示。
关于c# - 如何在启动 Windows 服务时读取 Windows 服务配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5594053/