0

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.

  • 1
    when you say "service" what exactly do you mean? a windows service, an IIS website, a console app? – Ewan Jul 13 '18 at 14:02
  • If the service is installed, and shows up in the list of services, why isn't it always running in the background? I thought that was the point of a Windows Service... – Greg Burghardt Jul 13 '18 at 16:36
2

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

  • I would add that the "Windows Scheduled Task" option means you can write the code any way you want, e.g. a bare bones console application would be just fine. The fact that it gets scheduled doesn't affect the code at all. – John Wu Jul 14 '18 at 2:46
1

I'm guessing we're talking about a Windows Service here.

Windows Services run continuously "in background" and can be started automatically when the server machine boots up. You need to configure the Service to do this.

There is absolutely nothing wrong with having a Timer that periodically calls your run() method (although you have to use the right one; there are about three to choose from). I've written many Windows Services that work in exactly this way.

  • Yes it starts after booting but, the service exits after a single run. How does it go through redoing run method after completing one round? My guess is a timer or something i discovered now is adding the service to scheduled tasks in windows. Can i do that during creation of the installer(add the service to scheduled tasks). Can u share me a place i could refer/read about that? – smned Jul 13 '18 at 12:44
  • @smned: Simple, you put a loop in your run method. You're overthinking this. – whatsisname Jul 13 '18 at 16:06
  • By all means put a loop into your run method - if you want the Service to eat up all the resources on your machine and slow it to a crawl. Using a Timer gives you two, big benefits: (1) Your service won't eat up the machine in a "busy wait" loop and (2) Your service will start cleanly because the OnStart method should only start the Timer and return, nice and quickly, which is what the Service Controller expects. Call the run method directly from OnStart and have run loop forever and the Service Controller will complain that your Service doesn't start properly. – Phill W. Jul 16 '18 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.