如何创建一个 Windows 服务应用程序的安装项目在 Visual C# 中

来源:互联网 发布:java 循环替换字符串 编辑:程序博客网 时间:2024/05/18 03:11

对于 Windows 服务创建安装项目

本节描述如何创建 Windows 服务项目,以及如何使用已编译的安装项目来安装 Windows 服务。

创建 Windows 服务项目

  1. 启动 Microsoft Visual Studio。
  2.  文件 菜单上指向 新建,然后单击 项目
  3.  项目类型 框中,单击 Visual C# 项目,然后单击 模板 下的 Windows 服务

    注意Visual Studio 2005 或 Visual Studio 2008 中展开 Visual C#项目类型 下,单击Windows,然后单击 模板 下的 Windows 服务
  4. 类型在 名称 文本框和然后键入在 位置 文本框中的 C:/LogWriterService。单击 确定
  5. 在解决方案资源管理器中,用鼠标右键单击 Service1.cs,然后单击 查看代码
  6.  OnStart 事件处理程序中,请使用下面的代码替换注释:
    EventLog.WriteEntry("My simple service started.");
  7. 在解决方案资源管理器中,双击 $ Service1.cs
  8. 在代码编辑器窗口中用鼠标右键单击 设计视图,然后单击 属性
  9. 在属性窗格中单击 添加安装程序 链接。
  10. 在为 ServiceInstaller1 属性窗格更改 ServiceName服务 1 的属性。
  11. 在设计视图中的代码编辑器窗口中单击 $ ServiceProcessInstaller1
  12. 在属性窗格中将 帐户 属性改为 本地系统 LocalService  网络服务 值是仅在 Microsoft Windows XP 中可用)。

安装 Windows 服务使用经过编译的安装项目

您完成在上一节可以配置 Windows 服务项目的步骤之后,请按照下列步骤,以便可以安装服务应用程序打包服务应用程序的部署项目中添加操作:

  1. 将新项目添加到 LogWriterService 项目。若要这样做,请按照下列步骤操作:
    1. 在解决方案资源管理器中,右击 解决方案 LogWriterService (1 项目),指向 添加,然后单击 新建项目
    2. 单击 安装和部署项目  项目类型,然后单击 模板 下的 安装项目
    3.  名称 文本框中键入 ServiceSetup
    4. 类型 C:/位置 文本中框,然后再单击 确定
  2. 告诉部署项目包。若要这样做,请按照下列步骤操作:
    1. 在解决方案资源管理器中,用鼠标右键单击 ServiceSetup,指向 添加,然后再单击项目输出
    2.  添加项目输出组 对话框中将 项目 框中单击 LogWriterService
    3. 单击 主输出,然后单击 确定
  3. 对于正确的安装中添加仅主输出。若要向自定义操作,请按照下列步骤操作:
    1. 在解决方案资源管理器中,用鼠标右键单击 ServiceSetup、 再指向 视图,然后单击自定义操作
    2. 用鼠标右键单击 自定义操作,然后单击 添加自定义操作
    3. 单击 应用程序文件夹,然后单击 确定
    4. 单击 主输出来自 LogWriterService (活动),然后单击 确定

      请注意在 安装 提交 回滚  卸载 下显示的 主输出
  4. 默认状态下,安装项目不会包括在生成配置中。若要生成解决方案,使用下列方法之一:
    • 方法 1
      1. 用鼠标右键单击 LogWriterService,然后单击 生成器
      2. 用鼠标右键单击 ServiceSetup,然后单击 生成
    • 方法 2
      1.  生成 菜单上单击生成整个解决方案的 配置管理器
      2. 单击以选中 ServiceSetup 生成 复选框。
      3. 按 f7 键生成整个解决方案。在构建该解决方案,则必须可用服务的一个完整的安装程序包。
  5. 若要安装新生成的服务,ServiceSetup,用鼠标右键单击,然后单击 安装
  6. ServiceSetup 对话框中单击 下一步 三次。请注意该服务安装时就会出现一个进度栏。
  7. 已经安装了服务,单击 关闭

完成代码列表

Service1.cs

using System;using System.Collections;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.ServiceProcess;namespace LogWriterService{public class Service1 : System.ServiceProcess.ServiceBase{/// <summary> /// Required designer variable./// </summary>private System.ComponentModel.Container components = null;public Service1(){// The Windows.Forms Component Designer must have this call.InitializeComponent();// TODO: Add any initialization after the InitComponent call}// The main entry point for the processstatic void Main(){System.ServiceProcess.ServiceBase[] ServicesToRun;// More than one user service may run in the same process. To add// another service to this process, change the following line to// create a second service object. For example,////   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};//ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };System.ServiceProcess.ServiceBase.Run(ServicesToRun);}/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor./// </summary>private void InitializeComponent(){components = new System.ComponentModel.Container();this.ServiceName = "Service1";}/// <summary>/// Clean up any resources that are being used./// </summary>protected override void Dispose( bool disposing ){if( disposing ){if (components != null) {components.Dispose();}}base.Dispose( disposing );}/// <summary>/// Set things in motion so your service can do its work./// </summary>protected override void OnStart(string[] args){EventLog.WriteEntry("My simple service started.");} /// <summary>/// Stop this service./// </summary>protected override void OnStop(){// TODO: Add code here to perform any tear-down necessary to stop your service.}}}

验证它工作的

  1. 在 $ 控制面版中双击 管理工具,然后双击 服务
  2. 用鼠标右键单击 服务 1,然后单击 开始
  3. 使用下列方法之一验证在事件日志中会记录一个事件:
    • 方法 1
      1. 在 $ 控制面版中双击 管理工具,然后双击 事件查看器
      2. 在左窗格中单击 应用程序日志,然后找到事件日志中有您的服务,在右窗格中。
    • 方法 2
      1. 在服务器资源管理器中,展开 服务器,展开 ComputerName,展开 事件日志、 展开 应用程序,然后展开 服务 1。 记住 服务 1 是在的类不是该服务的名称本身。因此,服务 1 用作应用程序的名称。 (它是超出了本文介绍如何自定义名称的范围。
      2. 将光标移至日志条目。从顶部开始第二个条目应阅读"我的简单服务启动

 

 

引用http://support.microsoft.com/kb/816169/zh-cn

原创粉丝点击