您的位置:首页 > 其它

[转]怎么用.NET创建视窗系统服务

2010-11-15 16:41 369 查看
摘自:http://www.sudu.cn/info/html/edu/20070410/319720.html

我们将研究怎么创建一个作为视窗系统服务的应用程式。内容包含什么是视窗系统服务,怎么创建、安装和调试他们。会用到System.ServiceProcess.ServiceBase命名空间的类。


什么是视窗系统服务?

  视窗系统服务应用程式是一种需要长期运行的应用程式,他对于服务器环境特别适合。他没有用户界面,并且也不会产生所有可视输出。所有用户消息都会被写进视窗系统事件日志。计算机启动时,服务会自动开始运行。他们不要用户一定登录才运行,他们能在包括这个系统内的所有用户环境下运行。通过服务控制管理器,视窗系统服务是可控的,能终止、暂停及当需要时启动。

  视窗系统 服务,以前的NT服务,都是被作为视窗系统 NT操作系统的一部分引进来的。他们在视窗系统 9x及视窗系统 Me下没有。你需要使用NT级别的操作系统来运行视窗系统服务,诸如:视窗系统 NT、视窗系统 2000 Professional或视窗系统 2000 Server。举例而言,以视窗系统服务形式的产品有:Microsoft Exchange、SQL Server,更有别的如设置计算机时钟的视窗系统 Time服务。


创建一个视窗系统服务

  我们即将创建的这个服务除了演示什么也不做。服务被启动时会把一个条目信息登记到一个数据库当中来指明这个服务已启动了。在服务运行期间,他会在指定的时间间隔内定期创建一个数据库项目记录。服务停止时会创建最后一条数据库记录。这个服务会自动向视窗系统应用程式日志当中登记下他成功启动或停止时的记录。

  Visual Studio .NET能够使创建一个视窗系统服务变成相当简单的一件事情。启动我们的演示服务程式的说明概述如下。

1. 新建一个项目
2. 从一个可用的项目模板列表当中选择视窗系统服务
3. 设计器会以设计模式打开
4. 从工具箱的组件表当中拖动一个Timer对象到这个设计表面上 (注意: 要确保是从组件列表而不是从视窗系统窗体列表当中使用Timer)
5. 设置Timer属性,Enabled属性为False,Interval属性30000毫秒
6. 转换到代码视图页(按F7或在视图菜单当中选择代码),然后为这个服务填加功能


视窗系统服务的构成

  在你类后面所包含的代码里,你会注意到你所创建的视窗系统服务扩充了System.ServiceProcess.Service类。所有以.NET方式建立的视窗系统服务必须扩充这个类。他会需求你的服务重载下面的方法,Visual Studio默认时包括了这些方法。

• Dispose ? 清除所有受控和不受控资源(managed and unmanaged resources)
• OnStart ? 控制服务启动
• OnStop ? 控制服务停止

数据库表脚本样例

  在这个例子中使用的数据库表是使用下面的T-SQL脚本创建的。我选择SQL Server数据库。你能非常容易修改这个例子让他在Access或所有你所选择的别的数据库下运行。

CREATE TABLE [dbo].[MyServiceLog] (
[in_LogId] [int] IDENTITY (1, 1) NOT NULL,
[vc_Status] [nvarchar] (40)
COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[dt_Created] [datetime] NOT NULL
) ON [PRIMARY]

视窗系统服务样例

  下面就是我命名为MyService的视窗系统服务的所有原始码。大多数原始码是由Visual Studio自动生成的。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ServiceProcess;

namespace CodeGuru.My视窗系统Service
{
public class MyService : System.ServiceProcess.ServiceBase
{
private System.Timers.Timer timer1;
/// <remarks>
/// Required designer variable.
/// </remarks>
private System.ComponentModel.Container components = null;

public MyService()
{
// This call is required by the 视窗系统.Forms
// Component Designer.
InitializeComponent();
}

// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new MyService() };

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()
{
this.timer1 = new System.Timers.Timer();
((System.ComponentModel.ISupportInitialize)
(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Interval = 30000;
this.timer1.Elapsed +=
new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// MyService
//
this.ServiceName = "My Sample Service";
((System.ComponentModel.ISupportInitialize)
(this.timer1)).EndInit();

}

/// <summary>
/// Clean up any resources 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)
{
this.timer1.Enabled = true;
this.LogMessage("Service Started");
}

/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
this.timer1.Enabled = false;
this.LogMessage("Service Stopped");
}

/*
* Respond to the Elapsed event of the timer control
*/
private void timer1_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
this.LogMessage("Service Running");
}

/*
* Log specified message to database
*/
private void LogMessage(string Message)
{
SqlConnection connection = null;
SqlCommand command = null;
try
{
connection = new SqlConnection(
"Server=localhost;Database=SampleDatabase;Integrated
Security=false;User Id=sa;Password=;");
command = new SqlCommand(
"INSERT INTO MyServiceLog (vc_Status, dt_Created)
VALUES (’" + Message + "’,getdate())", connection);
connection.Open();
int numrows = command.ExecuteNonQuery();
}
catch( Exception ex )
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
finally
{
command.Dispose();
connection.Dispose();
}
}
}
}

安装视窗系统服务

  视窗系统服务不同于普通视窗系统应用程式。不可能简简单单地通过运行一个EXE就启动视窗系统服务了。安装一个视窗系统服务应该通过使用.NET Framework提供的InstallUtil.exe来完成,或通过诸如一个Microsoft Installer (MSI)这样的文件部署项目完成。


添加服务安装程式

  创建一个视窗系统服务,仅用InstallUtil程式去安装这个服务是不够的。你必须还要把一个服务安装程式添加到你的视窗系统服务当中,这样便于InstallUtil或是所有别的安装程式知道应用你服务的是怎样的设置设置。

1. 将这个服务程式转换到设计视图
2. 右击设计视图选择“添加安装程式”
3. 转换到刚被添加的ProjectInstaller的设计视图
4. 设置serviceInstaller1组件的属性:
1) ServiceName = My Sample Service
2) StartType = Automatic
5. 设置serviceProcessInstaller1组件的属性
1) Account = LocalSystem
6. 生成解决方案

  在完成上面的几个步骤之后,会自动由Visual Studio产生下面的原始码,他包含于ProjectInstaller.cs这个源文件内。

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;

namespace CodeGuru.My视窗系统Service
{
/// <summary>
/// Summary description for ProjectInstaller.
/// </summary>
[RunInstaller(true)]
public class ProjectInstaller :
System.Configuration.Install.Installer
{
private System.ServiceProcess.ServiceProcessInstaller
serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public ProjectInstaller()
{
// This call is required by the Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new
System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new
System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "My Sample Service";
this.serviceInstaller1.StartType =
System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new
System.Configuration.Install.Installer[]
{this.serviceProcessInstaller1, this.serviceInstaller1});
}
#endregion
}
}

用InstallUtil安装视窗系统服务

  目前这个服务已生成,你需要把他安装好才能使用。下面操作会指导你安装你的新服务。

1. 打开Visual Studio .NET命令提示
2. 改动路径到你项目所在的bin\Debug目录位置(如果你以Release模式编译则在bin\Release目录)
3. 执行命令“InstallUtil.exe My视窗系统Service.exe”注册这个服务,使他建立一个合适的注册项。
4. 右击桌面上“我的计算机”,选择“管理”就能打计算机管理控制台
5. 在“服务和应用程式”里面的“服务”部分里,你能发现你的视窗系统服务已包含在服务列表当中了
6. 右击你的服务选择启动就能启动你的服务了

  在每次需要修改视窗系统服务时,这就会需求你卸载和重新安装这个服务。不过要注意在卸载这个服务前,最佳确保服务管理控制台已关闭,这会是个非常好的习惯。如果没有这样操作的话,你可能在卸载和重安装视窗系统服务时会遇见麻烦。仅卸载服务的话,能执行相的InstallUtil命令用于注销服务,不过要在后面加一个/u命令开关。


调试视窗系统服务

  从另外的角度度看,调试视窗系统服务绝不同于一个普通的应用程式。调试视窗系统服务需求的步骤更多。服务不能象你对普通应用程式做的那样,只要简单地在研发环境下执行就能调试了。服务必须首先被安装和启动,这一点在前面部分我们已做到了。为了便于跟踪调试代码,一旦服务被启动,你就要用Visual Studio把运行的进程附加进来(attach)。记住,对你的视窗系统服务做的所有修改都要对这个服务进行卸载和重安装。


附加正在运行的视窗系统服务

  为了调试程式,有些附加视窗系统服务的操作说明。这些操作假定你已安装了这个视窗系统服务并且他正在运行。

1. 用Visual Studio装载这个项目
2. 点击“调试”菜单
3. 点击“进程”菜单
4. 确保 显示系统进程 被选
5. 在 可用进程 列表中,把进程定位于你的可执行文件名称上点击选中他
6. 点击 附加 按钮
7. 点击 确定
8. 点击 关闭
9. 在timer1_Elapsed方法里设置一个断点,然后等他执行


总结

  目前你应该对视窗系统服务是什么,及怎么创建、安装和调试他们有一个粗略的认识了。视窗系统服务的额处的功能你能自行研究。这些功能包括暂停(OnPause)和恢复(OnContinue)的能力。暂停和恢复的能力在默认情况下没有被启用,要通过视窗系统服务属性来设置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: