.net - C# Running console application as windows service - The service didn't respond error -


i'm trying run console app windows service, followed this question, , made few changes make fit app.

my main code looks that:

public static class program {     public class service : servicebase     {          public service(string servicename)         {             this.servicename = servicename;         }          protected override void onstart(string[] args)         {             program.start(args);         }          protected override void onstop()         {             program.stop(this.servicename);         }     }     #endregion      static void main(string[] args)     {         if (!environment.userinteractive)             // running service             using (var service = new service("testservice"))                 servicebase.run(service);         else         {             // running console app             start(args);         }     }      private static void start(string[] args)     {         while(true)         {             //do somthing         }     }      private static void stop(string servicename)     {         //writing log 'servicename' stopped     } } 

i tried run following console app service, using following steps:

1) use command: sc create servicetestname123 binpath= "path exe file in project debug folder".

2) use command: sc start servicetestname123 "parameter1".

and got error: "startservice failed 1053: service did not respond start or control request in timely fashion"

i read error in internet , found out can try solve problem running start function thread, updated onstart function following function:

protected override void onstart(string[] args) {     thread t = new thread(() => program.start(args));     t.start(); } 

after trying re-create service (delete old 1 , create service again new onstart function) , re-run got same error.

by way, when ran code console app worked properly.

could please explaing me doing wrong?

thanks lot.

i tried exact steps , worked me. highlight few key points came across

  1. onstart should return in timely fashion. i.e. work should happen in separate process/thread. used code thread , worked fine me.
  2. make sure executable on local drive can accessed "local system" account without permission issues.
  3. make sure when create service, provide absolute path , not relative path.
  4. make sure sc create ... command responds [sc] createservice success
  5. check service created in service control panel
  6. make sure can start service control panel before attempting command line
  7. also, open task manager or process explorer make sure service executable running or not (irrespective of status returned service control panel or scm start)
  8. for debugging, logged information local temp file - again watch out permissions issues local system account.
  9. if whatever reasons have delete service, make sure indeed disappeared service control panel

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -