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