7

I have developed a windows service using Topshelf. it works fine locally. When i deployed to test and try to start the service, it is giving me the following error:

Error 1053: The service did not respond to the start or control request in a timely fashion.

The test server is running on Windows server 2012.

This is my service start and stop methods:

public void Start()
{
    _logProvider.Info("Service started.");

    StartScheduledJobs();
}

public void Stop()
{
    _scheduler.Shutdown(true);

    _logProvider.Info("Service stopped.");
}

private void StartScheduledJobs()
{
    try
    {
        _scheduler.Start();

         ScheduleDeleteJob();
    }
    catch (Exception ex)
    {
        _logProvider.Error("", ex);
    }
}

Can anyone help me what could be the reason with the solution please?

Thanks

Mukil Deepthi
  • 6,072
  • 13
  • 71
  • 156
  • Please post your solution, since so many different errors can occur. This will help others. – Jeremy Ray Brown Feb 02 '19 at 14:20
  • Before doing anything examine your application event log in windows. That might save you some time before choosing between trying threading or the other solutions. In my case log4net was not found in system32 folder which is solved by @DanieBoy below. – Per Johansson May 25 '20 at 11:44
  • You really should mark an answer. – Danieboy Dec 04 '20 at 05:56

8 Answers8

6

After trying every other solution without success I found a solution that solved my problem with the same error message.

public static int Main(string[] args)
{
    Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
    //...
}

Based on https://github.com/Topshelf/Topshelf/issues/473

Danieboy
  • 4,393
  • 6
  • 32
  • 57
3

The problem is that you are starting the service's work in the Start() method.

This works fine during development, but when you install a service, the Service Control manager calls Start and waits 30 seconds for it to return - if it does, then the service is considered to have installed successfully.

Because you start your scheduled jobs, though, in the Start method, it doesn't return within that time, and you get this error.

The resolution is to start the work indirectly in Start and then return - start a dedicated thread to do it, or use a timer, there are lots of options.

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • Hello could you please take a look on my question: https://stackoverflow.com/questions/63522054/topshelf-service-wont-start-as-a-service-error-1053-the-service-did-not-respond – Roxy'Pro Aug 21 '20 at 11:36
  • I did, but as you're already using a timer and the code looks OK, I don't see what I can contribute there. – stuartd Aug 22 '20 at 02:30
3

Here this issue has been resolved: https://github.com/Topshelf/Topshelf/issues/183

In a nutshell: your Start() method should return true when service starts.

Eugene Rozhkov
  • 663
  • 6
  • 10
1

My Situation was a lot easier to resolve and so simple!

The Service Parameters in Program.cs did not match the Parameters used to install the service in the ServiceInstaller.

So basically I matched this in Program.cs:

                x.SetServiceName(serviceName);
                x.SetDisplayName(serviceDisplayName);
                x.SetDescription(serviceDescription);

To the Parameters set in the Service installer.

Voila! Fixed the problem.

  • Same here. My service was RabbitMQ listener and also all it took was to set the same attribute values that were used during SC INSTALL – Wiizl Oct 28 '22 at 12:18
0

I'm not sure the error message you're receiving is the actual error. How to debug:

  • Verify with logs that start method begins. Note: make sure log path is specific, since windows service may be starting under different account. If you have something like ${LOCALAPPDATA} in your log file appender path then you'll be confused why you don't any logs in the path you've been using during development.
  • Verify that more than 30 seconds elapsed before StartScheduledJobs() finishes.
  • If your code in Start is not executing, then something in static void main(args []) is happening before the window's service starts that is causing this issue. This issue may only happen when doing MyService.exe start. I recommend removing code prior to where HostFactory wraps your service.

Note: TopShelf has log4net plugin you can install to trace where the exception is occuring (http://docs.topshelf-project.com/en/latest/configuration/logging.html).

Jeremy Ray Brown
  • 1,499
  • 19
  • 23
0

I was in the same situation... After two days of debugging one colleague saw that my path to quartz_jobs.xml in App.config was incorrect.

incorrect

<add key="quartz.plugin.jobInitializer.fileNames" value="quartz_jobs.xml" />

correct

<add key="quartz.plugin.xml.fileNames" value="~/quartz_jobs.xml" />

Cheers!

chunk1ty
  • 400
  • 3
  • 14
0

When I first learned to use Topshelf, there's no mention on speeding up the start method. Below if my start method from before

public void Start()
{ //instatntiate a bunch of object here, taking up too much time }

This is how I changed my code to speed up the Start method

public void Start()
{  RunStartUpTasksAysnc();}  //put a reference of start instructions on stack then exit
private async Task RunStartUpTasksAysnc()
{  
    await Task.Run(()=>{//instatntiate a bunch of object here});
}

Instead of running a bunch of constructors taking up the alloted time to start the service, it puts the tasks on the stack to be run asynchronously

0

In my case, the problem was caused by this line

Console.Title = "Some application";
Adam
  • 1,796
  • 18
  • 19