C# service from console application

507 Views Asked by At

I have a Console Application that every x minutes makes an API call and, based off the response, control some IoT switches.
I need this to run 24/24 on a windows machine. I figured out that running it as a console application the risk is that the timer could broke over time.
How can I convert this app to a windows service without rewriting it from zero? I also implemented some console-logging stuff, so I'd like to run the application as console, and have a service to generate the timer elapsed events, if it's possible.

Ps: The timer interval is something between 2 to 5 minutes, and the program run only during day (it checks the photovoltaic production and the API request limit is 300 per day, so I check only when sun is actually out)

1

There are 1 best solutions below

3
Lajos Arpad On

If the application is well-written, then the application logic is separated from the user-interface (CLI in this case) and you could simply implement only the UI in your Windows app and reuse the same classes that you were using in the initial project.

If the prerequisite above is not met, that is, the application logic is mixed up with the user interface handler, then you will need to refactor the initial project so you will end up with a project where the app logic is separated from the user interface handler.