Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a C# console application as a Windows service

I have a basic C# console application that I would like to run as a Windows Service.

I have created the Windows service using sc create. This worked fine, and I can see my service under services.msc. When I try and start this service I get the following error:

Could not start the PROJECT service on Local Computer. Error 1053: The service did not respond to the start or control request in a timely fashion.`

I read that this might be due to the service code taking longer than 30000 ms to execute. Therefore I removed the bulk of the code so that hardly anything is being executed.. yet the same error persists.

I am using .NET 3.5 for this project.

What would cause this?

like image 631
wulfgarpro Avatar asked Jun 03 '11 01:06

wulfgarpro


2 Answers

You cannot just take any console application and run as Windows service. First you need to implement your service class that would inherit from ServiceBase, then in entry point (Main) you need to run the service with ServiceBase.Run(new YourService()). In your service class you need to define what happens when service starts and ends.

Ideally you should add ServiceInstaller to your assembly too. This way you will be able to preset your service properties, and use installutil.exe to install the service.

like image 81
Alex Aza Avatar answered Sep 27 '22 23:09

Alex Aza


Walkthrough: Creating a Windows Service Application in the Component Designer is a walkthrough of how to create a service.

like image 39
Sani Singh Huttunen Avatar answered Sep 28 '22 00:09

Sani Singh Huttunen