C#Windows服务开发.docx

上传人:b****6 文档编号:4725066 上传时间:2022-12-08 格式:DOCX 页数:14 大小:262.55KB
下载 相关 举报
C#Windows服务开发.docx_第1页
第1页 / 共14页
C#Windows服务开发.docx_第2页
第2页 / 共14页
C#Windows服务开发.docx_第3页
第3页 / 共14页
C#Windows服务开发.docx_第4页
第4页 / 共14页
C#Windows服务开发.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

C#Windows服务开发.docx

《C#Windows服务开发.docx》由会员分享,可在线阅读,更多相关《C#Windows服务开发.docx(14页珍藏版)》请在冰豆网上搜索。

C#Windows服务开发.docx

C#Windows服务开发

用VisualC#创建Windows服务程序

一.Windows服务介绍:

  Windows服务以前被称作NT服务,是一些运行在WindowsNT、Windows2000和WindowsXP等操作系统下用户环境以外的程序。

在以前,编写Windows服务程序需要程序员很强的C或C++功底。

然而现在在VisualStudio.Net下,你可以运用C++或VisualC#或VisualBasic.Net很轻松的创建一个Windows服务程序。

同样,你还可以运用其他任何与CLR相容的语言来创建Windows服务程序。

本文就向大家介绍如何运用VisualC#来一步一步创建一个文件监视的Windows服务程序,然后介绍如何安装、测试和调试该Windows服务程序。

  在介绍如何创建Windows服务程序以前,我先向大家介绍一些有关Windows服务的背景知识。

一个Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序。

Windows服务程序虽然是可执行的,但是它不像一般的可执行文件通过双击就能开始运行了,它必须有特定的启动方式。

这些启动方式包括了自动启动和手动启动两种。

对于自动启动的Windows服务程序,它们在Windows启动或是重启之后用户登录之前就开始执行了。

只要你将相应的Windows服务程序注册到服务控制管理器(ServiceControlManager)中,并将其启动类别设为自动启动就行了。

而对于手动启动的Windows服务程序,你可以通过命令行工具的NETSTART命令来启动它,或是通过控制面板中管理工具下的服务一项来启动相应的Windows服务程序(见图1)。

同样,一个Windows服务程序也不能像一般的应用程序那样被终止。

因为Windows服务程序一般是没有用户界面的,所以你也要通过命令行工具或是下面图中的工具来停止它,或是在系统关闭时使得Windows服务程序自动停止。

因为Windows服务程序没有用户界面,所以基于用户界面的API函数对其是没有多大的意义。

为了能使一个Windows服务程序能够正常并有效的在系统环境下工作,程序员必须实现一系列的方法来完成其服务功能。

Windows服务程序的应用范围很广,典型的Windows服务程序包含了硬件控制、应用程序监视、系统级应用、诊断、报告、Web和文件系统服务等功能。

  图1

  二.创建Windows服务程序:

在介绍如何创建Windows服务程序以前,我先向大家介绍一下.Net框架下与Windows服务相关的命名空间和其中的类库。

.Net框架大大地简化了Windows服务程序的创建和控制过程,这要归功于其命名空间中的功能强大的类库。

和Windows服务程序相关的命名空间涉及到以下两个:

System.ServiceProcess和System.Diagnostics。

要创建一个最基本的Windows服务程序,我们只需要运用.Net框架下的System.ServiceProcess命名空间以及其中的四个类:

ServiceBase、ServiceInstaller、ServiceProcessInstaller以及ServiceController,其体系结构可见图2。

  图2

  其中ServiceBase类定义了一些可被其子类重载的函数,通过这些重载的函数,服务控制管理器就可以控制该Windows服务程序了。

这些函数包括:

OnStart()、OnStop()、OnPause()以及OnContinue()等四个。

而且ServiceBase类的子类还可以重载OnCustomCommand()函数来完成一些特定的操作。

通过重载以上的一些函数,我们就完成了一个Windows服务程序的基本框架,这些函数的重载方法如下:

protectedoverridevoidOnStart(string[]args)

{

}

protectedoverridevoidOnStop()

{

}

protectedoverridevoidOnPause()

{

}

protectedoverridevoidOnContinue()

{

}

  ServiceBase类还为我们提供了一些属性,而这些属性是任何Widnows服务程序所必须的。

其中的ServiceName属性指定了Windows服务的名称,通过该名称系统就可以调用Windows服务了,同时其它应用程序也可以通过该名称来调用它的服务。

而CanPauseAndContinue和CanStop属性顾名思义就是允许暂停并恢复和允许停止的意思。

  要使得一个Windows服务程序能够正常运行,我们需要像创建一般应用程序那样为它创建一个程序的入口点。

在Windows服务程序中,我们也是在Main()函数中完成这个操作的。

首先我们在Main()函数中创建一个Windows服务的实例,该实例应该是ServiceBase类的某个子类的对象,然后我们调用由基类ServiceBase类定义的一个Run()方法。

然而Run()方法并不就开始了Windows服务程序,我们必须通过前面提到的服务控制管理器调用特定的控制功能来完成Windows服务程序的启动,也就是要等到该对象的OnStart()方法被调用时服务才真正开始运行。

如果你想在一个Windows服务程序中同时启动多个服务,那么只要在Main()函数中定义多个ServiceBae类的子类的实例对象就可以了,方法就是创建一个ServiceBase类的数组对象,使得其中的每个对象对应于某个我们已预先定义好的服务。

{

System.ServiceProcess.ServiceBase[]MyServices;

MyServices=newSystem.ServiceProcess.ServiceBase[]{newService1(),newService2()};

System.ServiceProcess.ServiceBase.Run(MyServices);

}

staticvoidMain()

  三.添加文件监视服务:

  了解了Windows服务的基本体系结构和创建方法后,我们就可以试着往服务中添加一些实际的功能了。

下面我将向大家介绍一个能监视本地文件系统的文件监视服务-FileMonitorService。

该服务能根据预先设定的本地目录路径监视其中的文件包括子文件夹中的任何变化:

文件创建、文件删除、文件改名、文件修改。

同时,该服务还为每种变化创建了一个相对应的计数器,计数器的作用就是反映该种变化的频度。

  首先,我们打开VisualStudio.Net,新建一个VisualC#的Windows服务的项目,如图3所示:

  图3

  在重载Windows服务的OnStart()函数之前,我们先给其类添加一些计数器对象,这些计数器分别对应了文件的创建、删除、改名以及修改等变化。

一旦指定目录中的文件发生以上的某种变化,与其相对应的计数器就会自动加1。

所有的这些计数器都是定义为PerformanceCounter类型的变量的,该类是包含在System.Diagnostics命名空间中的。

privateSystem.Diagnostics.PerformanceCounterfileCreateCounter;

privateSystem.Diagnostics.PerformanceCounterfileDeleteCounter;

privateSystem.Diagnostics.PerformanceCounterfileRenameCounter;

privateSystem.Diagnostics.PerformanceCounterfileChangeCounter;

  之后我们便在类的InitializeComponent()方法中创建以上定义的各个计数器对象并确定其相关属性。

同时我们将该Windows服务的名称设置为“FileMonitorService”,设定其即是允许暂停并恢复的又是允许停止的。

privatevoidInitializeComponent()

             {

                    ponents=newSystem.ComponentModel.Container();

                    this.fileChangeCounter=newSystem.Diagnostics.PerformanceCounter();

                    this.fileDeleteCounter=newSystem.Diagnostics.PerformanceCounter();

                    this.fileRenameCounter=newSystem.Diagnostics.PerformanceCounter();

                    this.fileCreateCounter=newSystem.Diagnostics.PerformanceCounter();

 

                    fileChangeCounter.CategoryName="FileMonitorService";

                    fileDeleteCounter.CategoryName="FileMonitorService";

                    fileRenameCounter.CategoryName="FileMonitorService";

                    fileCreateCounter.CategoryName="FileMonitorService";

 

                    fileChangeCounter.CounterName="FilesChanged";

                    fileDeleteCounter.CounterName="FilesDeleted";

                    fileRenameCounter.CounterName="FilesRenamed";

                    fileCreateCounter.CounterName="FilesCreated";

 

                    this.ServiceName="FileMonitorService";

                    this.CanPauseAndContinue=true;

                    this.CanStop=true;

                    servicePaused=false;

             }

  接着就是重载OnStart()函数和OnStop()函数,OnStart()函数完成了一些必要的初始化工作。

在.Net框架下,文件的监视功能可以由FileSystemWatcher类来完成,该类是包含在System.IO命名空间下的。

该Windows服务所要完成的功能包括了监视文件的创建、删除、改名和修改等变化,而FileSystemWatcher类包含所有了对应于这些变化的处理函数。

protectedoverridevoidOnStart(string[]args)

             {    

                    FileSystemWatchercurWatcher=newFileSystemWatcher();

 

                    curWatcher.BeginInit();

                    curWatcher.IncludeSubdirectories=true;

                    curWatcher.Path=

            System.Configuration.ConfigurationSettings.AppSettings

               ["FileMonitorDirectory"];

                    curWatcher.Changed+=newFileSystemEventHandler(OnFileChanged);

                    curWatcher.Created+=newFileSystemEventHandler(OnFileCreated);

                    curWatcher.Deleted+=newFileSystemEventHandler(OnFileDeleted);

                    curWatcher.Renamed+=newRenamedEventHandler(OnFileRenamed);

                    curWatcher.EnableRaisingEvents=true;

                    curWatcher.EndInit();

             }

  注意其中被监视的目录是存放在一个应用程序配置文件中的,该文件是一个XML类型的文件。

这种做法的好处就是我们不必重新编译并发布该Windows服务而只要直接修改其配置文件就可以达到更改所要监视的目录的功能了。

  当该Windows服务启动后,一旦被监视的目录中的文件发生某种变化,与其相对应的计数器的值便会相应的增加,方法很简单,只要调用计数器对象的IncrementBy()即可。

privatevoidOnFileChanged(Objectsource,FileSystemEventArgse)

             {

                    if(servicePaused==false)

                    {

                           fileChangeCounter.IncrementBy

(1);

                    }

             }

 

             privatevoidOnFileRenamed(Objectsource,RenamedEventArgse)

             {

                    if(servicePaused==false)

                    {

                           fileRenameCounter.IncrementBy

(1);

                    }

             }

 

            privatevoidOnFileCreated(Objectsource,FileSystemEventArgse)

             {

                    if(servicePaused==false)

                    {

                           fileCreateCounter.IncrementBy

(1);

                    }

             }

 

            privatevoidOnFileDeleted(Objectsource,FileSystemEventArgse)

             {

                    if(servicePaused==false)

                    {

                           fileDeleteCounter.IncrementBy

(1);

                    }

             }

  OnStop()函数即是停止Windows服务的,在该Windows服务中,服务一旦停止,所有的计数器的值都应归零,但是计数器并不提供一个Reset()方法,所以我们只好将计数器中的值减去当前值来达到这个目的。

protectedoverridevoidOnStop()

             {

                    if(fileChangeCounter.RawValue!

=0)

                    {

                           fileChangeCounter.IncrementBy(-fileChangeCounter.RawValue);

                    }

                    if(fileDeleteCounter.RawValue!

=0)

                    {

                           fileDeleteCounter.IncrementBy(-fileDeleteCounter.RawValue);

                    }

                    if(fileRenameCounter.RawValue!

=0)

                    {

                           fileRenameCounter.IncrementBy(-fileRenameCounter.RawValue);     

                    }

                    if(fileCreateCounter.RawValue!

=0)

                    {

                           fileCreateCounter.IncrementBy(-fileCreateCounter.RawValue);

                    }

             }

  同时,因为我们的Windows服务是允许暂停并恢复的,所以我们还得重载OnPause()函数和OnContinue()函数,方法很简单,只要设定前面定义的布尔值servicePaused即可。

protectedoverridevoidOnPause()

             {

                    servicePaused=true;

             }

 

            protectedoverridevoidOnContinue()

             {

                    servicePaused=false;

            }

  这样,该Windows服务的主体部分已经完成了,不过它并不有用,我们还必须为其添加安装文件。

安装文件为Windows服务的正确安装做好了工作,它包括了一个Windows服务的安装类,该类是重System.Configuration.Install.Installer继承过来的。

安装类中包括了Windows服务运行所需的帐号信息,用户名、密码信息以及Windows服务的名称,启动方式等信息。

[RunInstaller(true)]

      publicclassInstaller1:

System.Configuration.Install.Installer

      {

             ///

             ///必需的设计器变量。

             ///

             privateSystem.ComponentModel.Containercomponents=null;

             privateSystem.ServiceProcess.ServiceProcessInstallerspInstaller;

             privateSystem.ServiceProcess.ServiceInstallersInstaller;

 

             publicInstaller1()

             {

                    //该调用是设计器所必需的。

                    InitializeComponent();

 

                   //TODO:

在InitComponent调用后添加任何初始化

             }

 

             #regionComponentDesignergeneratedcode

             ///

             ///设计器支持所需的方法-不要使用代码编辑器修改

             ///此方法的内容。

             ///

            privatevoidInitializeComponent()

             {

                    components=newSystem.ComponentModel.Container();

 

                    //创建ServiceProcessInstaller对象和ServiceInstaller对象

                    this.spInstaller=

              newSystem.ServiceProcess.ServiceProcessInstaller();

                    this.sInstall

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 其它课程

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1