Search code examples
c#windows-services

Windows Service "Timeout" instantly on startup


I'm currently running in to an issue where a Windows Service I wrote is "timing out" instantly on start up. The message I get is Error 1053: The service did not respond to the start or control request in a timely fashion. I checked Event Viewer and I see that message and another A timeout was reached (30000 milliseconds) while waiting for the X service to connect. Only problem is that it's not waiting 30 seconds to time out, it's more like half a second.

My service's 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;
    }

My entire service works splendidly in our development and certification environments, but won't start in our production environment, it doesn't seem like it's even getting to the OnStart() because a log is never made. Things I've checked:

  1. Made sure service had correct permissions in the necessary directories, it does
  2. Made sure the correct version of .NET framework that my service is targeting (4) is installed, it is
  3. Made sure Event Viewer wasn't throwing any other types of errors that might give me a hint to what's happening, there's nothing
  4. All of the directories for the FileSystemWatcher actually exist, they do
  5. The directory for the log4net file exists, it does

I'm at a loss at the moment; any help would be awesome.

edit After double checking the .NET framework again I realize I checked the wrong server for the versions. A good way to be certain is to double-click on the actual exe file for the service and see what it says. In my case it literally said "Make sure 4.0 is installed" which prompted me to check again and there I saw that 4.0 wasn't installed.


Solution

  • Are you sure that your .net is up to date. This could happen if for instance 3.5 is on the machine and you're using 4.0.