c#开发和部署windows服务_第1页
c#开发和部署windows服务_第2页
c#开发和部署windows服务_第3页
c#开发和部署windows服务_第4页
c#开发和部署windows服务_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

1、c#开发和部署windows服务c#创建Windows服务(Installutil .exe 方式加载)一.Windows服务介绍:Windows服务以前被称作NT服务,是一些运行在WindowsNT Windows2000 和Windows XP等操作系统下用户环境以外的程序。在以前,编写Windows服务程序需要程序员很强的 C或C+创底。然而现在在Visual Studio.Net下,你可 以运用C+域 Visual C#或Visual Basic.Net很轻松的创建一个 Windows服务程序。同样,你还可以运用其他任何与CLR相容的语言来创建Windows服务程序。本文就向大家 介绍

2、如何运用Visual C#来一步一步创建一个文件监视的 Windows服务程序,然后介绍如何安装、测试和调试该Windows服务程序。在介绍如何创建Windows服务程序以前,我先向大家介绍一些有关Windows 服务的背景知识。一个Windows服务程序是在Windows操作系统下能完成特定 功能的可执行的应用程序。Windows服务程序虽然是可执行的,但是它不像一般 的可执行文件通过双击就能开始运行了,它必须有特定的启动方式。这些启动方式包括了自动启动和手动启动两种。对于自动启动的Windows服务程序,它们在Windows启动或是重启之后用户登录之前就开始执行了。只要你将相应的Windo

3、ws服务程序注册到服务控制管理器(Service Control Manager )中,并 将其启动类别设为自动启动就行了。而对于手动启动的Windows服务程序,你可以通过命令行工具的NET STAR怖令来启动它,或是通过控制面板中管理工 具下的服务一项来启动相应的 Windows服务程序(见图1)。同样,一个Windows 服务程序也不能像一般的应用程序那样被终止。因为Windows服务程序一般是 没有用户界面的,所以你也要通过命令行工具或是下面图中的工具来停止它, 或是在系统关闭时使得 Windows服务程序自动停止。因为 Windows服务程序没 有用户界面,所以基于用户界面的 API

4、函数对其是没有多大的意义。为了能使 一个Windows服务程序能够正常并有效的在系统环境下工作,程序员必须实现 一系列的方法来完成其服务功能。Windows服务程序的应用范围很广,典型的 Windows服务程序包含了硬件控制、应用程序监视、系统级应用、诊断、报告、 We侨口文件系统服务等功能。二.创建 Windows服务程 序:在介绍如何创建 Windows服务程序以前,我先向大家介绍一下.Net框架下与 Windows服务相关的命名空间和其中的类库。.Net框架大大地简化了 Windows 服务程序的创建和控制过程,这要归功于其命名空间中的功能强大的类库。和 Windows服务程序相关的命名

5、空间涉及到以下两个:System.ServiceProcess和 System.Diagnostics 。要创建一个最基本的 Windows服务程序,我们 只需要运用.Net框架下的 System.ServiceProcess 命名空间以及其中的四个类:ServiceBase、Serviceinstaller 、ServiceProcessInstaller 以及 ServiceController.其中ServiceBase类定义了一些可被其子类重载的函数,通过这些重载的函 数,服务控制管理器就可以控制该 Windows服务程序了。这些函数包 括:OnStart ()、OnStop()、On

6、Pause()以及 OnContinue ()等四个。而且 ServiceBase 类的子类还可以重载OnCustomCommand)函数来完成一些特定的操作。通过 重载以上的一些函数,我们就完成了一个 Windows服务程序的基本框架,这些 函数的重载方法如下:protected override void OnStart(string口 args) protected override void OnStop() protected override void OnPause() protected override void OnContinue() ServiceBase类还为我们提供

7、了一些属性,而这些属性是任彳sj Widnows服务程序所必须的。其中的ServiceName属性指定了 Windows服务的名称,通过该 名称系统就可以调用Windows服务了,同时其它应用程序也可以通过该名称来 调用它的服务。而 CanPauseAndContinue和CanStop属性顾名思义就是允许暂 停并恢复和允许 停止的意思。要使得一个Windows服务程序能够正常运行,我们需要像创建一般应用程序那样为它创建一个程序的入口点。在 Windows服务程序中,我们也是在 Main()函数中完成这个操作的。首先我们在Main ()函数中创建一个 Windows®务的实例,该实例

8、应该是 ServiceBase类的某个子类的对象,然后我们调用由 基类ServiceBase类定义的一个Run ()方法。然而Run ()方法并不就开始了 Windows服务程序,我们必须通过前面提到的服务控制管理器调用特定的控制功 能来完成Windows服务程序的启动,也就是要等到该对象的OnStart ()方法被调用时服务才真正开始运行。如果你想在一个Windows服务程序中同时启动多个服务,那么只要在 Main ()函数中定义多个ServiceBae类的子类的实例 对象就可以了,方法就是创建一个ServiceBase类的数组对象,使得其中的每个对象对应于某个我们已预先定义好的服务。Sys

9、tem.ServiceProcess.ServiceBase口 MyServices;MyServices = new System.ServiceProcess.ServiceBase口 new Service1(), new Service2() ;System.ServiceProcess.ServiceBase.Run(MyServices); 三。示例(这个服务在启动和停止时,向一个文本文件中写入一些文字信息。)第一步:创建服务框架要创建一个新的 Windows服务,可以从 Visual C# 工程中选取 Windows服务 (WindowsService )选项,给工程一个新文件名

10、 WindowsService ,然后点击 确你可以看到,向导向工程文件中增加WebService1.cs类:其中各属性的含意是:Autolog志文件CanHandlePowerEventCanPauseAndContinue请求是否自动写入系统的日服务时候接受电源事件服务是否接受暂停或继续运行的CanShutdown服务是否在运行它的计算机关闭时收到通知,以便能 够调用OnShutDown过程CanStop服务是否接受停止运行的请求ServiceName月艮务名第二步:向服务中增加功能在.cs代码文件中我们可以看到,有两个被忽略的函数OnStart和OnStopoOnStart函数在启动服务

11、时执 行,OnStop函数在停止服务时执行。在这里,当 启动和停止服务时,向一个文本文件中写入一些文字信息,代码如下: view plaincopy to clipboardprint?1. using System;2. using System.Collections.Generic;3. using System.ComponentModel;4. using System.Data;5. using System.Diagnostics;6. using System.Linq;7. using System.ServiceProcess;8. using System.Text;9.

12、 using System.IO;9.namespace WindowsServicepublicpartial class Service1 : ServiceBasepublic Service1() InitializeComponent();string strPath = "d:Test.txt"args)21. 22. FileStream fs = new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write);23.24. StreamW

13、riter m_streamWriter = newStreamWriter(fs);25.26. m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);27.28.rvice: tring() 8.39.40.rPath, 41.m_streamWriter.WriteLine("mcWindowsSe Service Started" + DateTime.Now.ToS + "n");m_streamWriter.Flush();m_streamW

14、riter.Close();fs.Close();protected override void OnStop() FileStream fs = new FileStream(st FileMode.OpenOrCreate, FileAccess.Write);42.StreamWriter(fs);43.44.ekOrigin.End);45.46.Service:Service"n");0.51.52.StreamWriter m_streamWriter = newm_streamWriter.BaseStream.Seek(0, Sem_st

15、reamWriter.WriteLine(" mcWindow:Stopped " + DateTime.Now.ToString() +m_streamWriter.Flush();m_streamWriter.Close();fs.Close();54. 55. 56. usingSystem;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Linq;using System.Servi

16、ceProcess;using System.Text;using System.IO;namespace WindowsServicepublic partial class Service1 : ServiceBasepublic Service1()InitializeComponent();第三步:将安装程序添加到服务应用程序这样,该Windows服务的主体部分已经完成了,不过它并不有用,我们还必须为其添加安装文件。安装文件为 Windows服务的正确安装做好了工作,它 包括了一个Windows服务的安装类,该类是重 System.Configuration.Install.Insta

17、ller继承过来的。安装类中包括了Windows服务运行所需的帐号信息,用户 名、密码信息以及 Windows服务的名 称,启动方式等信息。Visual Studio.NET 随附有安装组件,可用来安装与服务应用程序相关联的资 源。安装组件在正在安装到的系统上注册一项单个的服务,并使服务控制管理 器知道该服务的存在。要正确安装服务,并不需要在安装程序中进行任何特殊编码。但是,如果需要 向安装进程添加特殊功能,则可能偶尔需要修改安装程序的内容。将安装程序添加到服务应用程序的步骤是:1:在解决方案中,访问要向其中添加安装组件的服务的Design视图。2:在属性窗口中,单 击添加安装程序这时项目中就

18、添加了一个新类 ProjectInstaller和两个安装组件ServiceProcessInstaller 和 ServiceInstaller ,并且服务的属性值被复制到 组件。3:若要确定如何启 动服务,请单击 ServiceInstaller组件并将StartType属性设置为适当的值。Manual服务安装后,必须手动启动。Automatic 每次计算机重新启动时,服务都会自动启动。Disabled服务无法启动。4:将 serviceProcessInstaller类的Account属性改为LocalSystem这样,不论是以哪个用户登录的系统,服务总会启动view plaincopy

19、 to clipboardprint?1. namespace WindowsService2. (3. partialclass Installer14. (.9.nstaller/ <summary>/必需的设计器变量。/ </summary>/private System.ServiceProcess.ServiceProcessI spInstaller;10.private System.ServiceProcess.ServiceInstaller sInstaller;

20、5.36.private System.ComponentModel.IContainer co mponents = null;/ <summary>/清理所有正在使用的资源。/ </summary>/ <param name="disposing"& gt;如果应释放托 管资源,为 true ;否则为 false 。 </param>protected override void Dispose(bool disp osing)if (disposi

21、ng && (components != null) components.Dispose(); base.Dispose(disposing);#region组件设计器生成的代码/ <summary>/设计器支持所需的方法-不要/使用代码编辑器修改此方法的内容。/ </summary> private void InitializeComponent() 37.ServiceInstaller 对象/ 创建 ServiceProcessInstaller 对象和38.this.spInstallernew System.Servcomponents =

22、 new System.ComponentM odel.Container();iceProcess.ServiceProcessInstaller();39.this.sInstallernew System.ServiceProcess.ServiceInstaller();40.41. / 设定 ServiceProcessInstaller 对象的帐号、用户名和密码等信息42. this.spInstaller.Account = System.ServiceProcess.ServiceAccount.LocalSystem;43. this.spInstaller.Username

23、= null;44. this.spInstaller.Password= null;45.46. / 设定服务名称47. this.sInstaller.ServiceName= "WindowsService"48.49. / 设定服务的启动方式50. this.sInstaller.StartType= System.ServiceProcess.ServiceStartMode.Automatic;51.52. this.Installers.AddRange(new System.Configuration.Install.Installer口 this.spInstaller, this.sInstaller );53. 54.55. #endregion56. 57. namespaceWindowsServicepartial class Installed/summary/必禽的设计器变量。/summary/private Syst

温馨提示

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

最新文档

评论

0/150

提交评论