Running a C# console application as a Windows service

14,100

Solution 1

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.

Solution 2

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

Share:
14,100
wulfgarpro
Author by

wulfgarpro

I'm paid to be some kind of, "Software Developer". An aspiring infosec practitioner, hobbiest hacker and perpetual learner, I own a subpar website, underutilised twitter account, and an empty github.

Updated on July 02, 2022

Comments

  • wulfgarpro
    wulfgarpro almost 2 years

    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?

  • Alex Aza
    Alex Aza almost 13 years
    @WulfgarPro - because windows service application has a little different architecture. You need to define what to do when service starts and ends.
  • Alex Aza
    Alex Aza almost 13 years
    sc create is how you can install windows service, even if you don't have installer inside of your assembly.
  • Luis Perez
    Luis Perez over 10 years
    There are tool that will let you run a console application as a service without modification. For example this one: runasservice.com