C# – Best Way to Implement Windows Service

cserviceswindows

I have written a C# service using MVC (No view).

The program starts execution and processes some data from database and stores some aggregated data and works just fine in VB whenever i run it.

What i have an issue with is deploying it to production. Whenever i prepare an installation and run it on production server a service is installed and shows up on the list of services.

What i am trying to achieve is make the service timely or always run in the background so it wouldn't involve me running the service manually.

I read that time count services are a bad idea(running with set time count down and invoking the run method).

What are recommended ways of deploying such a service which does database aggregation and populating timely.

Best Answer

2 ways, with a few considerations:

Deploy as a Windows Service

For this, for simplicity sake, I'd recommend something like http://topshelf-project.com/ which will simplify the deployment process for you.

Once deployed, you should expect to see the service startup set to 'Automatic' given your requirements. No doubt you will need to configure topshelf prior to building your solution. So in the code for your service you can set a timer to regularly run whatever piece of functionality is it you want running. That said, given the fact you want this running regularly, I'd also seriously consider...

Windows Scheduled Task

This will take care of the timing issue for you. There is no need to reinvent the wheel when it comes to scheduling the execution of your code, Windows has this built in for you.

https://www.digitalcitizen.life/how-create-task-basic-task-wizard