C# 主项目和服务
尽管我进行了彻底的谷歌搜索,但我仍然对此事感到困惑。
让我解释一下我的情况。我创建了 ac# 项目,它使用户能够手动备份数据库(通过单击按钮)。现在,用户必须能够安排数据库备份自动运行的时间。为了实现这一目标,我计划创建一个服务,当达到设置的备份时间时,该服务通过 Windows 调度程序启动。
我需要将服务与主项目一起部署(在我看来,该服务将是一个不同的项目。也许我错了。)。
我的问题是当用户安装主项目时如何部署服务?
PS:我使用的是c#express和sql 2008 R2express。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用什么来安装主应用程序。您所说的打包工具很可能也有允许您安装服务的钩子。例如,如果您使用 Wix 创建 msi 包来安装主应用程序,则可以将 Wix 配置为也安装该服务。
此 Google 搜索将为您提供相同内容的相关文章。
如果您没有使用任何安装程序工具,假设您只是向用户提供包含所有可执行文件的 zip,那么您将需要手动安装该服务。 本文非常适合创建可自行安装的服务。您可以使用 Process.Start执行
installutil
exe来实际安装/卸载服务。编辑1:
根据 Rup 对您的问题的评论,您已经拥有备份数据库所需的代码。您所需要的只是能够安排此活动。用户在 UI 中输入计划后,您可以在任务计划程序中创建相应的任务。让该任务执行您的主应用程序,传入参数
-backup "dbName"
或前面提到的备份代码运行所需的任何信息。您可以使用以下模板 [这适用于控制台应用程序,但也适用于您的 GUI 应用程序。您需要检查的是是否传入了任何开关,然后不要启动 gui,而是简单地执行备份功能代码。] ...还有很多 现有 有关 StackOverflow 的问题 使用哪个命令行解析工具.. 。
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 ...
我采取的方法是为 UI 创建项目,为服务创建项目。该服务将是一个始终运行并负责创建计划任务的 Windows 服务。 (而不是让计划任务启动服务。)如何创建计划任务是相当开放的,您可以使用
AT
,或者可以与 TaskScheduler 类型库进行一些 COM 互操作。我希望这有帮助。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.