简体   繁体   中英

installing a windows service

I have created a Windows Service in ASP.NET 4.0 and I am using the following command to install the service after starting a command prompt as administrator:

C:\Windows\system32>sc create EnviroTracker1 binpath= "D:\Freelance Work\SuperExpert\git EnviroTrack\EnviroTrack\EnviroTrackerService\bin\Release\EnviroTrackService.exe"

[SC] CreateService SUCCESS

After that, I go to Administrator tools -> services, and try to start the service, but I'm getting the following error:

 ---------------------------

Services

---------------------------

Windows could not start the EnviroTracker1 service on Local Computer.

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

 ---------------------------

OK

---------------------------

I also tried installutil . In that case also, service is installed and its start type is automatic and service status is automatic in services property window. Please suggest me a solution to this.

Thanks.

Edit: As you mentioned in one of the comments, the Thread.Sleep is causing the OnStart to wait for some 100+ seconds, resulting in the error message.

Incase you are unable to stop the service using sc stop EnviroTracker1 , you could mark the service for deletion. The service will be gone after a machine restart. sc delete EnviroTracker1


http://support.microsoft.com/kb/839174

An error 1053 means that the service spent more than 30 secs in the OnStart method. Thats way too much time. Personally in my service OnStart, I simply start a timer with an interval of 2 seconds. I do all the work that I would have normally done in OnStart in the elapsed function of the timer. This way my initialization can take its own sweet time and the OnStart can complete within a second.

To help you solve your specific problem, we will need specifics. You mentioned somewhere that it seems to be a permissions issue. What made you feel that. More to the point, why dont you put in some logging statements or exceptional handling. If an exception is being thrown, then the stack trace will help. The logging statements will help you and in turn us locate the problem point.

Infact you could also attach a debugger to the service . As it seems to be stuck in OnStart for 30 secs, you ought to have ample time to attach the visual studio debuger to the service instance.

Maybe you could post the code of the OnStart method... You know, help us help you:)

You will need have a class that derives from ServiceBase and add code to the OnStart and OnStop methods. Once you get that working you can right click anywhere in designer view and choose "Add Installer" which will add the necessary code to the assembly that allows installutil to register the service.

public class YourService : ServiceBase
{
  public static void Main(string[] args)
  {
    ServiceBase.Run(new ServiceBase[] { new YourService() });
  }

  protected overrides void OnStart(string[] args)
  {
    // Add code to start your logic here. Try to return immediately.
  }

  protected overrides void OnStop()
  {
    // Add code to stop your logic here.
  }
}

It is possible that in the OnStart method you are waiting too long to return. You need to make sure that you are only doing enough work to get the logic of your service started. This means you may have to spin up a new thread or otherwise somehow start your logic asynchronously.

A few months ago I needed to create a windows service and I found this LINK and THIS one very helpful.

Amith George's answer goes to the root cause of this problem but if you're looking for how do I debug the OnStart of a windows service I usually add the following because you may not be fast enough to manually attach to the service.

     protected override void OnStart(string[] args)
     {
        if (args.Contains("DEBUG_SERVICE))
            DebugMode();


         #if DEBUG
             DebugMode();
         #endif



        }

    private static void DebugMode()
    {

        Debugger.Break();
    }

Brian Gideon also makes a good point about needing to "spin up a new thread or otherwise somehow start your logic asynchronously". Unless you're responding to a system event or listening on a network port use should consider using a timer for this. My answer has a sample that I use as a template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM