Search code examples
c#.netwindows-servicestopshelf

C# Topshelf TimeoutException


As a First step I created Windows Service project configured it properly and

On second Step I have added TopShelf Version 3.1.135.0 in my project If I run my service through (F5 Run) then it is loading Top-shelf Console and service is completed successfully.

However When I am running it to install and Start it from command prompt I am having below TimeOut Error.

Topshelf.Hosts.StartHost Error: 0 : The service failed to start., System.Service
Process.TimeoutException: Time out has expired and the operation has not been co
mpleted.



 public class AppService
    {
        LoggingService loggingService = new LoggingService(typeof(AppService).Name);


        public void Start()
        {
            loggingService.Info("SampleService is Started");
            ExtractProcess.Start();
            TransformProcess.Start();

        }

        public void Stop()
        {
            loggingService.Info("SampleService is Stopped");

        }
    }

-- Updated Code to fix this issue

 public void Start()
    {
        loggingService.Info("MPS.GOA.ETLService  is Started");
        ThreadStart myThreadDelegate = new ThreadStart(StartService);
        Thread myThread = new Thread(myThreadDelegate);
        myThread.Start();

    }

private void StartService()
{
    timer.Elapsed += new System.Timers.ElapsedEventHandler(OnElapsedTime);
    timer.Interval = 60000 * ServiceIntervalInMinutes;     //1 minute 60000 milliseconds
    timer.Enabled = true;
    Process();
}

private void Process()
{
    ExtractProcess.Start();
    TransformProcess.Start();
}

Any Suggestions? Time Out Error


Solution

  • This error is happening because you are running the extract and process methods in the Start method of the service. This is OK in Visual Studio, but when you install the service and start it, the Service Control Manager waits for the Start method to return, and if it does not do so within a certain time (30 seconds by default) then it will return this error.

    You have several options, all of which will allow the Start method to return immediately:

    1. Invoke the extract and transform methods on a separate thread
    2. Invoke the extract and transform methods asynchronously
    3. Use a timer to start the extract and transform process