现在的位置: 首页 > 综合 > 正文

如何在 VisualC # .NET 和 VisualC # 2005 中创建安装项目用于 Windows 服务应用程序

2013年08月19日 ⁄ 综合 ⁄ 共 3808字 ⁄ 字号 评论关闭

为 Windows 服务创建安装项目


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

创建 Windows 服务项目


1. 启动 MicrosoftVisualStudio.NET 或 Microsoft Visual Studio 2005。
2. 文件 菜单, 指向 新建 , 然后单击 项目
3. 单击 项目类型 , 下 VisualC # 项目 , 然后单击 模板 Windows 服务

注意 入 Visual Studio 2005, 展开 VisualC # 项目类型 下, 单击 Windows , 和然后单击 模板 的 Windows 服务

4. 名称 文本框中, 键入 LogWriterService , 然后在 位置 文本框中键入 C:/ 。 单击 确定
5. 在 SolutionExplorer@@, 右击 Service1.cs , 并单击 查看代码
6. OnStart 事件处理程序, 用以下代码替换该备注:

EventLog.WriteEntry("My simple service started.");

7. 在 SolutionExplorer@@, 双击 Service1.cs
8. 在代码编辑器窗口, 右击 设计视图 , 然后单击 属性
9. 在属性窗格中, 单击 @ @ AddInstaller@@ @ 链接。
10. 对于 ServiceInstaller 1 , 属性窗格中更改到 Service 1 ServiceName 属性。
11. 在设计视图, 代码编辑器窗口中单击 ServiceProcessInstaller1
12. LocalSystem (: LocalService NetworkService 值都仅用于 MicrosoftWindowsXP) 在属性窗格中, 更改 帐户 属性。

使用编译安装项目来安装 Windows 服务


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

1. 向 LogWriterService 项目添加新项目。 要这样做, 请按照下列步骤操作:

a. 在 SolutionExplorer@@, 右击 , 指向 添加 , 然后单击 新建项目 (1 项目) 解决方案 ' LogWriterService '。
b. 单击 安装和部署项目 ProjectTypes@@ , 下, 然后单击 模板 SetupProject@@@
c. 名称 文本框中, 键入 ServiceSetup
d. 位置 文本框中, 键入 C:/ , 然后单击 确定
2. 向包告诉部署项目什么。 要这样做, 请按照下列步骤操作:

a. 在 SolutionExplorer@@, 右击 ServiceSetup , 指向 添加 , 并单击 ProjectOutput@@@
b. 添加项目输出组 对话框中, 在 项目 中, 单击 LogWriterService
c. 单击 PrimaryOutput@@@ , 并单击 确定
3. 对于正确安装, 添加只有主输出。 要添加自定义操作, 请按照下列步骤:

a. 在 SolutionExplorer@@, 右击 ServiceSetup 、 指向 视图 , 并单击 @ @ CustomActions@@@
b. 右击 @ @ CustomActions@@@ , 然后单击 @ @ @ AddCustomAction@@@
c. 单击 ApplicationFolder@@ , 并单击 确定
d. 单击 主输出从 LogWriterService (Active) , 并单击 确定

注意, 安装 Commit Rollback Uninstall 下出现 主输出

4. 默认情况下, 安装项目不包含生成配置中。 来构建解决方案, 使用下列方法之一:

方法 1

a. 右击 LogWriterService , 然后单击 生成
b. 右击 ServiceSetup , 然后单击 生成
方法 2

a. 生成 菜单上, 单击 ConfigurationManager@@ 器生成整个解决方案。
b. 单击以选择对于 ServiceSetup 构建 复选框。
c. 按 F 7 键生成整个解决方案。 生成解决方案, 时您可以完成安装包, 可用于该服务。
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 process
		static 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. 右击 Service 1 , 并单击 开始
3. 使用下列方法之一来验证该事件日志中记录的事件:

方法 1

a. 在控制面板, 双击 管理工具 , 并双击 事件查看器
b. 在左窗格中, 单击 Application 日志 , 然后定位事件日志用于您服务从右窗格中。
方法 2

a. 在 ServerExplorer@@, 展开 服务器 ComputerName 展开 Application ,、 事件日志 和展开 Service 1 。 记住, Service 1 是类, 不服务本身的名称。 因此, Service 1 用作应用程序名称。 (它是超出了本文将解释如何自定义名称范围。
b. 将光标通过日志条目。 从顶部二项应阅读我简单服务 " 启动 "。

 

【上篇】
【下篇】

抱歉!评论已关闭.