1

Despite my thorough googling, I am still confused on this matter.

Let me explain my situation. I created a c# project which gives the user the ability to back up the database manually (via a button click). Now the user must be able to schedule a time at which the database back up will run automatically. To achieve this, I am planning on creating a service which is started via the windows scheduler when the set back up time is reached.

I will need the deploy the service along with the main project (In my head, the service will be a different project. Maybe I am wrong here.).

My question is how do I deploy the service when the user installs the main project?

PS: I am using c# express and sql 2008 R2 express.

seva titov
  • 11,720
  • 2
  • 35
  • 54
Yash
  • 2,259
  • 4
  • 26
  • 33
  • "service which is started via the windows scheduler" - do you mean an actual Windows service then, or a separate utilty .exe? (Or even code built in to your main .exe with a command-line switch to activate?) If you're using the scheduler you probably don't need to write a full service. – Rup May 15 '11 at 17:12
  • @Rup Good point, if the users that are using the application have the appropriate rights you can create the scheduled task remotely from the UI. @Yash You would probably still want some centralized DB to keep track of the scheduled tasks, unless we are only talking about a couple of databases. Otherwise it can get hard to track all of the tasks you have setup. – TheHurt May 15 '11 at 17:23
  • Rup can you give me more details or a link to some documentation about code built the main.exe with a command line switch? – Yash May 15 '11 at 17:24

2 Answers2

1

What are you using to install the main application. Chances are you the said packaging tool will also have hooks for allowing you to install the service. For example, if you are using Wix to create a msi package to install the main application, then you can configure Wix to also install the service.

This google search will point you to relevant articles for the same.

If instead you are not using any installer tool, say you simply give the user a zip containing all executables, then you will need to manually install the service. This article is perfect to create a self installable service. You could use Process.Start to execute the installutil exe to acutally install/uninstall the service.

Edit1: Building on Rup's comment to your question, you already have the code required to backup the database. All you need, is to be able to schedule this. Once the user enters a schedule in your UI, you can create the corresponding task in the Task Scheduler. Have that task execute your main application, passing in the argument -backup "dbName" or what ever info is needed for the before mentioned backup code to run.

You may use the following template [which is meant for a console app, but will work just fine for your gui app as well. All you will need to check is if any switches have been passed in, then do not start the gui, instead simple execute the backup function code.] ... There are also a lot of existing questions on StackOverflow on which commandline parsing tool to use ...

Community
  • 1
  • 1
Amith George
  • 5,806
  • 2
  • 35
  • 53
0

The approach I would take is create the project for the UI, create a project for the service. The service would be a windows service that would always be running and would be responsible for creating the scheduled task. (Rather than having a scheduled task start the service.) How you go about creating the scheduled task is fairly open, you could shell out AT, or you could do some COM interop with the TaskScheduler type libs. I hope this helps.

TheHurt
  • 1,580
  • 14
  • 27