我有一个 topshelf windows 服务,我想在其中进行一些检查(即是否存在 xml 文件),如果检查失败,我需要停止 windows 服务。
所以我尝试在 Start() 方法中进行检查,然后引发异常:
public void Start()
{
if (!File.Exists(_xmlFile) throw new FileNotFoundException();
// Do some work here if xml file exists.
}
但是,Windows 服务在异常之后作为一个进程保留,然后我必须在任务管理器中手动终止。
如果某些条件(即找不到文件)成立,有没有办法不运行服务?