用.NET创建Windows服务.doc_第1页
用.NET创建Windows服务.doc_第2页
用.NET创建Windows服务.doc_第3页
用.NET创建Windows服务.doc_第4页
用.NET创建Windows服务.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

用.NET创建Windows服务我们将研究如何创建一个作为Windows服务的应用程序。内容包含什么是Windows服务,如何创建、安装和调试它们。会用到System.ServiceProcess.ServiceBase命名空间的类。什么是Windows服务?Windows服务应用程序是一种需要长期运行的应用程序,它对于服务器环境特别适合。它没有用户界面,并且也不会产生任何可视输出。任何用户消息都会被写进Windows事件日志。计算机启动时,服务会自动开始运行。它们不要用户一定登录才运行,它们能在包括这个系统内的任何用户环境下运行。通过服务控制管理器,Windows服务是可控的,可以终止、暂停及当需要时启动。Windows 服务,以前的NT服务,都是被作为Windows NT操作系统的一部分引进来的。它们在Windows 9x及Windows Me下没有。你需要使用NT级别的操作系统来运行Windows服务,诸如:Windows NT、Windows 2000 Professional或Windows 2000 Server。举例而言,以Windows服务形式的产品有:Microsoft Exchange、SQL Server,还有别的如设置计算机时钟的Windows Time服务。创建一个Windows服务我们即将创建的这个服务除了演示什么也不做。服务被启动时会把一个条目信息登记到一个数据库当中来指明这个服务已经启动了。在服务运行期间,它会在指定的时间间隔内定期创建一个数据库项目记录。服务停止时会创建最后一条数据库记录。这个服务会自动向Windows应用程序日志当中登记下它成功启动或停止时的记录。Visual Studio .NET能够使创建一个Windows服务变成相当简单的一件事情。启动我们的演示服务程序的说明概述如下。1. 新建一个项目2. 从一个可用的项目模板列表当中选择Windows服务3. 设计器会以设计模式打开4. 从工具箱的组件表当中拖动一个Timer对象到这个设计表面上 (注意: 要确保是从组件列表而不是从Windows窗体列表当中使用Timer) 5. 设置Timer属性,Enabled属性为False,Interval属性30000毫秒6. 切换到代码视图页(按F7或在视图菜单当中选择代码),然后为这个服务填加功能Windows服务的构成在你类后面所包含的代码里,你会注意到你所创建的Windows服务扩充了System.ServiceProcess.Service类。所有以.NET方式建立的Windows服务必须扩充这个类。它会要求你的服务重载下面的方法,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 PRIMARYWindows服务样例下面就是我命名为MyService的Windows服务的所有源代码。大多数源代码是由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.MyWindowsService public class MyService : System.ServiceProcess.ServiceBase private System.Timers.Timer timer1; / / Required designer variable. / private System.ComponentModel.Container components = null; public MyService() / This call is required by the Windows.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); / / Required method for Designer support - do not modify / the contents of this method with the code editor. / 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(); / / Clean up any resources being used. / protected override void Dispose( bool disposing ) if( disposing ) if (components != null) components.Dispose(); base.Dispose( disposing ); / / Set things in motion so your service can do its work. / protected override void OnStart(string args) this.timer1.Enabled = true; this.LogMessage(Service Started); / / Stop this service. / 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(); 安装Windows服务Windows服务不同于普通Windows应用程序。不可能简简单单地通过运行一个EXE就启动Windows服务了。安装一个Windows服务应该通过使用.NET Framework提供的InstallUtil.exe来完成,或者通过诸如一个Microsoft Installer (MSI)这样的文件部署项目完成。添加服务安装程序创建一个Windows服务,仅用InstallUtil程序去安装这个服务是不够的。你必须还要把一个服务安装程序添加到你的Windows服务当中,这样便于InstallUtil或是任何别的安装程序知道应用你服务的是怎样的配置设置。1. 将这个服务程序切换到设计视图2. 右击设计视图选择“添加安装程序”3. 切换到刚被添加的ProjectInstaller的设计视图4. 设置serviceInstaller1组件的属性: 1) ServiceName = My Sample Service 2) StartType = Automatic5. 设置serviceProcessInstaller1组件的属性 1) Account = LocalSystem6. 生成解决方案在完成上面的几个步骤之后,会自动由Visual Studio产生下面的源代码,它包含于ProjectInstaller.cs这个源文件内。using System;using System.Collections;using System.ComponentModel;using System.Configuration.Install;namespace CodeGuru.MyWindowsService / / Summary description for ProjectInstaller. / RunInstaller(true) public class ProjectInstaller : System.Configuration.Install.Installer private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1; private System.ServiceProcess.ServiceInstaller serviceInstaller1; / / Required designer variable. / 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 / / Required method for Designer support - do not modify / the contents of this method with the code editor. / 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安装Windows服务现在这个服务已经生成,你需要把它安装好才能使用。下面操作会指导你安装你的新服务。1. 打开Visual Studio .NET命令提示2. 改变路径到你项目所在的binDebug文件夹位置(如果你以Release模式编译则在binRelease文件夹)3. 执行命令“InstallUtil.exe MyWindowsService.exe”注册这个服务,使它建立一个合适的注册项。4. 右击桌面上“我的电脑”,选择“管理”就可以打计算机管理控制台5. 在“服务和应用程序”里面的“服务”部分里,你可以发现你的Windows服务已经包含在服务列表当中了6. 右击你的服务选择启动就可以启动你的服务了在每次需要修改Windows服务时,这就会要求你卸载和重新安装这个服务。不

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论