【发布时间】:2020-08-08 03:25:04
【问题描述】:
我目前遇到了一个问题,即我编写的 Windows 服务在启动时会立即“超时”。我收到的消息是 Error 1053: The service did not respond to the start or control request in a timely fashion. 我检查了事件查看器,我看到了该消息和另一个 A timeout was reached (30000 milliseconds) while waiting for the X service to connect. 唯一的问题是它没有等待 30 秒超时,它更像是半秒。
我的服务的 OnStart()
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private string version = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion;
private string incomingProdFileLocation = ConfigurationManager.AppSettings["ProdIncomingFileLocation"];
private string incomingCertFileLocation = ConfigurationManager.AppSettings["CertIncomingFileLocation"];
//private string incomingCombFileLocation = ConfigurationManager.AppSettings["CombIncomingFileLocation"];
private string processedFileLocation = ConfigurationManager.AppSettings["ProcessedFileLocation"];
private string errorFileLocation = ConfigurationManager.AppSettings["ErrorFileLocation"];
FileSystemWatcher prodWatcher = new FileSystemWatcher();
FileSystemWatcher certWatcher = new FileSystemWatcher();
//FileSystemWatcher combWatcher = new FileSystemWatcher();
protected override void OnStart(string[] args) {
log.InfoFormat("Starting up Merchant Bulk Load Service v{0}", version);
if (verifyDirectories()) {
log.InfoFormat("Initialize Prod FileSystemWatcher() at {0}", incomingProdFileLocation);
prodWatcher.Path = incomingProdFileLocation;
prodWatcher.Filter = "*.csv";
prodWatcher.Created += ProdBulkLoadFileReceived;
prodWatcher.EnableRaisingEvents = true;
log.InfoFormat("Initialize Cert FileSystemWatcher() at {0}", incomingCertFileLocation);
certWatcher.Path = incomingCertFileLocation;
certWatcher.Filter = "*.csv";
certWatcher.Created += CertBulkLoadFileReceived;
certWatcher.EnableRaisingEvents = true;
/*log.InfoFormat("Initialize Comb FileSystemWatcher() at {0}", incomingCombFileLocation);
combWatcher.Path = incomingCombFileLocation;
combWatcher.Filter = "*.csv";
combWatcher.Created += CombBulkLoadFileReceived;
combWatcher.EnableRaisingEvents = true;*/
} else {
log.ErrorFormat("verifyDirectories() returned false. Service stopping");
this.Stop();
}
}
private bool verifyDirectories() {
// verify each of the necessary directories exists before setting up any FileSystemWatcher()s
if (!Directory.Exists(incomingProdFileLocation)) {
log.ErrorFormat("Incoming production file location {0} does not exist. Please create the directory or edit the configuration file.",
incomingProdFileLocation);
return false;
}
if (!Directory.Exists(incomingCertFileLocation)) {
log.ErrorFormat("Incoming cert file location {0} does not exist. Please create the directory or edit the configuration file.",
incomingCertFileLocation);
return false;
}
/*if (!Directory.Exists(incomingCombFileLocation)) {
log.ErrorFormat("Incoming combined file location {0} does not exist. Please create the directory or edit the configuration file.",
incomingCombFileLocation);
return false;
}*/
if (!Directory.Exists(processedFileLocation)) {
log.ErrorFormat("Processed file location {0} does not exist. Please create the directory or edit the configuration file.",
processedFileLocation);
return false;
}
if (!Directory.Exists(errorFileLocation)) {
log.ErrorFormat("Error file location {0} does not exist. Please create the directory or edit the configuration file.",
errorFileLocation);
return false;
}
return true;
}
我的整个服务在我们的开发和认证环境中运行良好,但不会在我们的生产环境中启动,它似乎甚至没有到达 OnStart() 因为从来没有生成日志。我检查过的东西:
- 确保服务在必要的目录中具有正确的权限,确实如此
- 确保安装了我的服务所针对的正确版本的 .NET 框架 (4),它是
- 确保事件查看器没有抛出任何其他类型的错误,这些错误可能会给我提示正在发生的事情,什么都没有
- FileSystemWatcher 的所有目录实际上都存在,它们确实存在
- log4net 文件的目录存在,确实存在
我现在不知所措;任何帮助都会很棒。
编辑 再次仔细检查 .NET 框架后,我意识到我检查了错误的服务器版本。确定的一个好方法是双击该服务的实际 exe 文件并查看它的内容。在我的情况下,它的字面意思是“确保已安装 4.0”,这促使我再次检查,我看到没有安装 4.0。
【问题讨论】:
-
我通常会在 OnStart 中放入一个 try catch 块,并将 log 放入 catch 块中,以便获取更多信息。
-
这是我的错误,当我检查 .NET 框架时,我不小心查看了我在生产环境中登录的其他服务器之一。经过仔细检查,它实际上确实停在 3.5,我现在就继续关闭它......
标签: c# windows-services