【问题标题】:Windows Service "Timeout" instantly on startupWindows 服务在启动时立即“超时”
【发布时间】: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() 因为从来没有生成日志。我检查过的东西:

  1. 确保服务在必要的目录中具有正确的权限,确实如此
  2. 确保安装了我的服务所针对的正确版本的 .NET 框架 (4),它是
  3. 确保事件查看器没有抛出任何其他类型的错误,这些错误可能会给我提示正在发生的事情,什么都没有
  4. FileSystemWatcher 的所有目录实际上都存在,它们确实存在
  5. log4net 文件的目录存在,确实存在

我现在不知所措;任何帮助都会很棒。

编辑 再次仔细检查 .NET 框架后,我意识到我检查了错误的服务器版本。确定的一个好方法是双击该服务的实际 exe 文件并查看它的内容。在我的情况下,它的字面意思是“确保已安装 4.0”,这促使我再次检查,我看到没有安装 4.0。

【问题讨论】:

  • 我通常会在 OnStart 中放入一个 try catch 块,并将 log 放入 catch 块中,以便获取更多信息。
  • 这是我的错误,当我检查 .NET 框架时,我不小心查看了我在生产环境中登录的其他服务器之一。经过仔细检查,它实际上确实停在 3.5,我现在就继续关闭它......

标签: c# windows-services


【解决方案1】:

您确定您的 .net 是最新的吗?例如,如果机器上安装了 3.5 而您使用的是 4.0,则可能会发生这种情况。

【讨论】:

    【解决方案2】:

    这是一张旧票,但我刚刚在我们作为应用程序的一部分创建的所有 Windows 服务(其中大约 10 个)上看到了相同的错误(尽管是“90000 毫秒”而不是 30000)。 .Net 框架已安装并正常运行。

    我检查了设置此 90000 毫秒(90 秒)限制的注册表设置。 在节点中,HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control ServicesPipeTimeout 的值为 90000(十进制)。

    我不需要它是 90000,我不知道为什么要这样设置,但它肯定没有等待 90 秒——它立即失败了。

    所以我将值修改为 30000 并重新启动服务器。

    所有服务开始成功启动或重新启动。

    【讨论】:

      猜你喜欢
      • 2011-06-01
      • 2010-09-18
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      相关资源
      最近更新 更多