Skip to content
This repository has been archived by the owner on Jul 26, 2022. It is now read-only.

Topshlef 3.1.107.0 Service not starting only on windows server 2012 #183

Closed
hkollner opened this issue May 26, 2014 · 13 comments
Closed

Topshlef 3.1.107.0 Service not starting only on windows server 2012 #183

hkollner opened this issue May 26, 2014 · 13 comments

Comments

@hkollner
Copy link

Hi Guys,

I am having a problem where my topshelf service is not starting on Windows server 2012 but starts on all other windows servers.

As soon as I start the windows service I am instantly presented with a 1053 error: Did not respond in a timely fashion. even though it did not try to run for 30 secs but rather 1 second.

Once again note that this same service runs on all other Windows servers except for 2012. My Start() method does not even get fired.

I have not idea where else to look.

I have tried the following:

  • Logging (NLog) - No exceptions are thrown
  • RequestAdditionalTime()
  • Permissions

After the logging failed I replaced all logging with writing to the windows event log. No errors aside from the error 1053.

Here's an example of my Main constructor.

static void Main()
{
try
{

            HostFactory.Run(x =>
            {
                x.Service<ApplicationStartup>(s =>
                {
                    s.ConstructUsing(name => new ApplicationStartup());
                    s.WhenStarted(tc => tc.Start());
                    s.WhenStopped(tc => tc.Stop());
                });
                x.RunAsLocalSystem();
                x.StartManually();
                x.SetDescription("SERVICE SOLUTION ENGINE");
                x.SetDisplayName("SERVICE SOLUTION ENGINE");
                x.SetServiceName("SERVICESOLUTIONENGINE");
            });
        }
        catch (Exception ex)
        {
            Logger.ErrorException("Main Registered an Exception.", ex);
            throw;
        }
    }

Your help is GREATLY appreciated.

@Turntwo
Copy link

Turntwo commented Jun 23, 2014

I am having a similar issue - I get 1053 almost immediately when trying to start the service. Runs fine if I just run the exe. Happens on 2 servers (one of which is a Win7 machine), but not any others. I did upgrade the software on both of those locations and users were locking files at the time (I closed the file locks to install). I uninstalled and reinstalled the service, and rebooted the server (hoping it would release any file locking issues), but still no luck.

@jackfoxy
Copy link

jackfoxy commented Nov 2, 2015

I have possibly the same issue. Service remains in start pending status forever, but the service is working fine. The annoying thing is I cannot stop the service from the local services console.

OS: Windows Server 2012 R2 DataCenter
TopShelf compiled for Any CPU, NOT Prefer 32-bit
Target .NET Framework 4.6

@AliShahrivarian
Copy link

AliShahrivarian commented Nov 3, 2015

I have the same issue in windows 7. Service remains in start pending and for stopping it I should use "taskkill".

@AliShahrivarian
Copy link

AliShahrivarian commented Nov 3, 2015

The interesting thing is the services running account. I have multiple services that one doesn't start on any account but local "local system account" and others don't start on that but others.
I'm receiving the "1053" error and for accounts, I'm receiving "access denied".

@AliShahrivarian
Copy link

AliShahrivarian commented Nov 3, 2015

After a lot of searches finally, I found the issue. The problem was in my "start" method. This method has to call a service that doesn't respond in time. so windows service throws the timeout exception. I've changed the method and now I'm calling the service in the thread.
now everything going well.

@jackfoxy
Copy link

jackfoxy commented Nov 3, 2015

@AliShahrivarian can you throw-up a quick line of code for me. I've been coding in F# so long my C# is very rusty. Just the simple C# TopShelf wrapper for my F# ddl is the only C# I've seriously looked at in a long time...thanks!

@jackfoxy
Copy link

jackfoxy commented Nov 3, 2015

So the "bug" is really in the sample code and documentation. Should show calling in a thread. Makes sense.

@AliShahrivarian
Copy link

AliShahrivarian commented Nov 4, 2015

Sorry for my late @jackfoxy .

The bottom code is from the topshelf documentation https://topshelf.readthedocs.org/en/latest/configuration/quickstart.html:

public class Program
{
    public static void Main()
    {
        HostFactory.Run(x =>                                 //1
        {
            x.Service<TownCrier>(s =>                        //2
            {
               s.ConstructUsing(name=> new TownCrier());     //3
               s.WhenStarted(tc => tc.Start());              //4
               s.WhenStopped(tc => tc.Stop());               //5
            });
            x.RunAsLocalSystem();                            //6

            x.SetDescription("Sample Topshelf Host");        //7
            x.SetDisplayName("Stuff");                       //8
            x.SetServiceName("Stuff");                       //9
        });                                                  //10
    }
}

The thing you'll get from the code commented //4 that the service started and your start method will run after service start but it's not the truth. s.WhenStarted is the same as the "start" button in Windows Service and your method will run in Windows Service start and should finish before the timeout and (should or not) return true (I didn't test return matter) for Windows Service.

Suppose you have this start method:

public bool start(){
    // to do other config which run fast

    while(true){
          // to do something forever
    }
    return true;
}

This method doesn't return anything to the Windows Service and works forever. Windows Service is waiting for a response and when it gets nothing it'll throw an exception.
I've changed the start method like this one:

public bool start(){
    // to do other configs which run fast

    var myThread = new Thread(new ThreadStart(foreverWhile));
    myThread.Start();
    return true;
}
public void foreverWhile(){
     while(true){
         // to do something forever
     }
}

The thread will work independently and service will start.
Hope this helps you.

Update:

For preventing the thread from working after service stop we use the Isbackground property. Then .NET Framework in application exits will kill any thread process with true IsBackground.

public bool start(){
    // to do other config which run fast

    var myThread = new Thread(new ThreadStart(foreverWhile));
    myThread.IsBackground = true;  // This line will prevent thread from working after service stop.
    myThread.Start();
    return true;
}
public void foreverWhile(){
     while(true){
         // to do something forever
     }
}

@phatboyg
Copy link
Contributor

phatboyg commented Nov 6, 2015

And technically you should signal your background thread when Stop is called so that it can exit cleanly.

@jackfoxy
Copy link

Thanks, all. This worked out well for me. I suggest adding this tip prominently in documentation. I'm sure a lot of installations must run into similar situations, and its easy to overlook threading.

@dcarr42
Copy link

dcarr42 commented Dec 9, 2015

@phatboyg if you could provide an example of how you would signal the thread on exit.

@diogoandrei
Copy link

diogoandrei commented Nov 1, 2016

I'm facing a similar problem here, my service works fine if I run on debug or release, but when I try to install and run via cmd the Start() method apparently is not being called.

MyService.cs

public bool Start()
        {
            var t = new Thread(new ThreadStart(Run));
            t.Start();

            return true;
        }

        public bool Stop()
        {
            return true;
        }

        private void Run()
        {
            while (true)
            {
                Logger.Info(interval.ToString());

                interval++;

                Thread.Sleep(1000);
            }
        }

@hsbeaumier
Copy link

Thanks Ali Shahrivarian! Your suggestion saved the day. Creating the looping process code on a separate thread worked. It is strange, because I did not have to do this on an older version of Windows using .net 4.7.2, but I had to do this on the latest version of windows using .net version 4.8.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants