什么是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 [PRIMARY]
Windows服务样例
下面就是我命名为MyService的Windows服务的所有源代码。大多数源代码是由Visual Studio自动生成的。
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Diagnostics;
usingSystem.ServiceProcess;
namespaceCodeGuru.MyWindowsService
{
publicclassMyService : System.ServiceProcess.ServiceBase
{
privateSystem.Timers.Timer timer1;
/**
///Required designer variable.
///
privateSystem.ComponentModel.Container components=null;
publicMyService()
{
//This call is required by the Windows.Forms
//Component Designer.
InitializeComponent();
}
//The main entry point for the process
staticvoidMain()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun=newSystem.ServiceProcess.ServiceBase[]
{newMyService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/**
///Required method for Designer support - do not modify
///the contents of this method with the code editor.
///
privatevoidInitializeComponent()
{
this.timer1=newSystem.Timers.Timer();
((System.ComponentModel.ISupportInitialize)
(this.timer1)).BeginInit();
//
//timer1
//
this.timer1.Interval=30000;
this.timer1.Elapsed+=
newSystem.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
//MyService
//
this.ServiceName="My Sample Service";
((System.ComponentModel.ISupportInitialize)
(this.timer1)).EndInit();
}
/**
///Clean up any resources being used.
///
protectedoverridevoidDispose(booldisposing )
{
if( disposing )
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/**
///Set things in motion so your service can do its work.
///
protectedoverridevoidOnStart(string[] args)
{
this.timer1.Enabled=true;
this.LogMessage("Service Started");
}
/**
///Stop this service.
///
protectedoverridevoidOnStop()
{
this.timer1.Enabled=false;
this.LogMessage("Service Stopped");
}
/**//*
* Respond to the Elapsed event of the timer control
*/
privatevoidtimer1_Elapsed(objectsender,
System.Timers.ElapsedEventArgs e)
{
this.LogMessage("Service Running");
}
/**//*
* Log specified message to database
*/
privatevoidLogMessage(stringMessage)
{
SqlConnection connection=null;
SqlCommand command=null;
try
{
connection=newSqlConnection(
"Server=localhost;Database=SampleDatabase;Integrated
Security=false;User Id=sa;Password=;");
command=newSqlCommand(
"INSERT INTO MyServiceLog (vc_Status, dt_Created)
VALUES ('" + Message + "',getdate())", connection);
connection.Open();
intnumrows=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 = Automatic
5. 设置serviceProcessInstaller1组件的属性
1) Account = LocalSystem
6. 生成解决方案
在完成上面的几个步骤之后,会自动由Visual Studio产生下面的源代码,它包含于ProjectInstaller.cs这个源文件内。
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Configuration.Install;
namespaceCodeGuru.MyWindowsService
{
/**
///Summary description for ProjectInstaller.
///
[RunInstaller(true)]
publicclassProjectInstaller :
System.Configuration.Install.Installer
{
privateSystem.ServiceProcess.ServiceProcessInstaller
serviceProcessInstaller1;
privateSystem.ServiceProcess.ServiceInstaller serviceInstaller1;
/**
///Required designer variable.
///
privateSystem.ComponentModel.Container components=null;
publicProjectInstaller()
{
//This call is required by the Designer.
InitializeComponent();
//TODO: Add any initialization after the InitComponent call
}
Component Designer generated code#regionComponent Designer generated code
/**
///Required method for Designer support - do not modify
///the contents of this method with the code editor.
///
privatevoidInitializeComponent()
{
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. 改变路径到你项目所在的bin\Debug文件夹位置(如果你以Release模式编译则在bin\Release文件夹)
3. 执行命令“InstallUtil.exe MyWindowsService.exe”注册这个服务,使它建立一个合适的注册项。
4. 右击桌面上“我的电脑”,选择“管理”就可以打计算机管理控制台
5. 在“服务和应用程序”里面的“服务”部分里,你可以发现你的Windows服务已经包含在服务列表当中了
6. 右击你的服务选择启动就可以启动你的服务了
在每次需要修改Windows服务时,这就会要求你卸载和重新安装这个服务。不过要注意在卸载这个服务前,最好确保服务管理控制台已经关闭,这会是一个很好的习惯。如果没有这样操作的话,你可能在卸载和重安装Windows服务时会遇到麻烦。仅卸载服务的话,可以执行相的InstallUtil命令用于注销服务,不过要在后面加一个/u命令开关。
调试Windows服务
从另外的角度度看,调试Windows服务绝不同于一个普通的应用程序。调试Windows服务要求的步骤更多。服务不能象你对普通应用程序做的那样,只要简单地在开发环境下执行就可以调试了。服务必须首先被安装和启动,这一点在前面部分我们已经做到了。为了便于跟踪调试代码,一旦服务被启动,你就要用Visual Studio把运行的进程附加进来(attach)。记住,对你的Windows服务做的任何修改都要对这个服务进行卸载和重安装。
附加正在运行的Windows服务
为了调试程序,有些附加Windows服务的操作说明。这些操作假定你已经安装了这个Windows服务并且它正在运行。
1. 用Visual Studio装载这个项目
2. 点击“调试”菜单
3. 点击“进程”菜单
4. 确保 显示系统进程 被选
5. 在 可用进程 列表中,把进程定位于你的可执行文件名称上点击选中它
6. 点击 附加 按钮
7. 点击 确定
8. 点击 关闭
9. 在timer1_Elapsed方法里设置一个断点,然后等它执行
总结
现在你应该对Windows服务是什么,以及如何创建、安装和调试它们有一个粗略的认识了。Windows服务的额处的功能你可以自行研究。这些功能包括暂停(OnPause)和恢复(OnContinue)的能力。暂停和恢复的能力在默认情况下没有被启用,要通过Windows服务属性来设置。
归纳一下步骤大概如下:
1.创建服务:
1.1我们应该创建windows服务类型的project,IDE会自动从serviceBase类继承一个类出来,在这个类里面有on_star和on_stop两个方法,可以在里面写入启动逻辑和停止逻辑
在设计视图下我们可以修改service的属性
Autolog 是否自动写入系统的日志文件
CanHandlePowerEvent 服务时候接受电源事件
CanPauseAndContinue 服务是否接受暂停或继续运行的请求
CanShutdown 服务是否在运行它的计算机关闭时收到通知,以便能够调用 OnShutDown 过程
CanStop 服务是否接受停止运行的请求
ServiceName 服务名
1.2选择服务组件,并切换到设计模式,右键->Add Installer,生成安装文件。安装文件中的属性修改可以参考下面的内容:
projectInstall参数修改Account(指定用户还是使用本地系统用户)
ServiceInstall参数修改:
描述名:Description,
显示名:DisplayName,
ServiName:服务名,要和刚才建立的服务名一一对应才行
StartType:(Manual:服务安装后,必须手动启动.Automatic:每次计算机重新启动时,服务都会自动启动;Disabled:服务无法启动)
2安装
安装服务要使用微软提供的工具InstallUtil,这个工具在c:/windows/wicrosoft.net/framework/[version]中
使用InstallUtil 安装,使用InstallUtil /u 反安装服务
如果还有什么可以参考刚才说的那几个blog,ok一切都很简单。但是如果要做为一个企业级的应用,往往会有别的场景需求,下面说一下我曾碰到的场景要求
1.我们的windows服务可能不止满足一种业务逻辑,比如:如果你的机器安装了oracle的话,就会有OracleService服务,我们启动数据库的时候可以只是启动tns服务和OracleService服务就可以了,但是实际上启动OracleService服务的同时会启动好几个服务,例如:DBWR(数据文件写入),LGWR(日志文件写入),SMON(系统监护),PMON(用户进程监),CKPT(检查点,同步数据文件,日志文件,控制文件等)。有时为了性能的考虑,一个服务也可能要多线程的运行业务逻辑。比如一个导入文件的数据接口服务,如果数据量非常大就要这样考虑。一般如果执行多个任务,可以有两种方法:1.开多个线程;2.在一个线程中顺序进行(可以加上时间控制)。我觉得这里最灵活的方法是用.net本省的多线程支持来解决。
2.要能在不改变系统结构的情况下,可以非常快速的支持新的服务要求。这说明我们在onstar方法中不能写入相关类的处理逻辑,必须要用某种方法解耦才行。
实现方法:
参考下面的类图
servcive类和具体的业务实现类解耦,我们可以使用工厂模式生成业务逻辑类,每个业务逻辑类启动在一个新的线程中,在.net中工厂模式可以用反射非常方便的实现,这块代码如下:
typeInfo=node.Attributes["type"].Value;
Type type=Type.GetType(typeInfo);
IService instance=(IService)Activator.CreateInstance(type);
//初始化服务
instance.Initialize(node);
instanceArray.Add(instance);
//在新线程中运行服务,每个服务使用相同的安全上下文
ThreadStart ts=newThreadStart(instance.Start);
Thread t=newThread(ts);
t.Start();
从配置文件中读出需要加载的业务逻辑类,实例化后用ThreadStart调用其中的Start方法,启动新线程。这里对配置文件的读取如果要求比较高可以采用Enterprise Libary的Login block来做。
在OnStop方法中调用每个类的Stop方法,如果想对每个Stop方法异步调用,可以用delegate封装接口的Stop方法用
BeginInvoke方法实现异步调用
foreach(objectoininstanceArray)
{
try
{
IService service=(IService)o;
if(service!=null)
{
//异步调用每个服务的stop方法来停止业务组件
OnStopDelegate osd=newOnStopDelegate(service.Stop);
osd.BeginInvoke(null,null);
}
}
catch(Exception ex)
{
//
}
}
这样,整个windows服务就实现了,这里还有个额外的好处是可以热部署,即不需要删除已经部署的windows服务,将新的DLL覆盖将以前的DLL覆盖,修改*.exe.config文件并重新启动就可以了。其实我觉得停止服务,重新编译部署后在启动服务花费的时间可能比这样还少些。
关于调试:
windows服务常用的调试方法是用调试->附加到进程的方法。
选择显示所有用户进程,我们已经部署并启动的windows服务进程就会出现,并跳到我们设置好的断点处。但是这样对service中的onstart部分调试比较麻烦。我还是推荐创建一个porjec做为调试用,这里面可以写onstart部分的代码,代码不多,非常方便。大家就可以象调试普通windows程序一样调试这windows服务代码了
微软对此在这两篇文章那个中给出了相应的解决方案,http://support.microsoft.com/kb/820639/zh-cn,http://support.microsoft.com/kb/842793/zh-cn。
实际上就是在使用Windows.Forms命名空间中的timer无效时则使用System.Timers中的组件,再不行的话使用Threading中的Timer组件。我遇到的是第一种情况,通过使用System.Timers中的Timer,问题解决了。
症状
您添加到 Windows 服务 Microsoft WindowsForms 计时器组件。 启用计时器, 然后您设置计时器以引发事件的间隔。 计算机, 上安装 Windows 服务, 然后您启动服务。 不引发 计时器 事件在 Windows 服务。
注意: WindowsForms 计时器组件位于 System.Windows.Forms命名空间中。
原因
WindowsForms 计时器组件用于 WindowsForms 环境。 WindowsForms 计时器组件不用于服务器环境。 计时器如果在 Windows 服务使用因此, 可能不引发事件。
解决方案
要解决此问题, 使用服务器计时器从命名空间 System.Timers 代替 WindowsForms 计时器从 System.Windows.Forms 命名空间。 要这样做, 请按照下列步骤操作:
1.
在要删除 Service 1 类命令窗口运行以下命令:
installutil /u WindowsService1.exe注意WindowsService1.exe 位于您的项目文件夹下 bin文件夹中。 添加路径是您 WindowsService1.exe。
2.
切换到 Windows 服务项目。
3.
在 Service 1 设计器窗口, 单击 Timer 1 控件。
4.
按 DELETE 键从设计器窗口中删除该 Timer 1 控件。
5.
在工具栏上, 单击 组件 选项卡。
6.
从工具栏, 将 Timer 控件拖到设计器窗口。
7.
双击设计器窗口以查看代码窗口上 Timer 1 控件。
8.
Timer 1 控件的 Tick 事件处理程序中移动到的 Timer 1 控件 Elapsed 事件处理程序代码
9.
移除 Tick 事件处理程序。
10.
在 生成 菜单上, 单击 BuildSolution@@@ 。
11.
在命令窗口运行以下命令:
installutil WindowsService1.exe
12.
在 管理工具 菜单上, 单击 服务 。
13.
右击 Service 1 , 然后单击 开始 。
14.
等待几秒钟, 右击 Service 1 , 然后单击 停止 。
15.
打开, C:\sample.txt 文件, 然后注意文本。
以 开始 和 停止 Tick 文本显示。
状态
此行为是设计使然。
更多信息
再现问题的步骤
1.
启动 MicrosoftVisualStudio.NET。
2.
通过使用 MicrosoftVisualBasic.NET 或 MicrosoftVisualC # .NET 打开一个新 Windows 服务项目。
默认情况下, 创建 Service 1 。
3.
在工具箱中, 单击 WindowsForms 选项卡。
4.
将一个 Timer 控件从工具箱拖到 Service 1 设计器窗口。
5.
在设计器窗口, 双击 Timer 1 控件。
显示 Service 1 代码窗口。
6.
如果您使用 VisualC # .NET, 将以下语句添加到开头的代码:using System.IO;
7.
以下代码添加到 Service 1 类的 OnStart 过程。
VisualBasic.NET 代码'Set the interval of the timer to 3 seconds.
Timer1.Interval = 3000
Timer1.Enabled = True
'Open the sample.txt file in append mode.
FileOpen(1, "C:\sample.txt", OpenMode.Append)
'Print text to the "C:/sample.txt file.
Print(1, "Start")
FileClose()VisualC # .NET 代码//Set the interval of timer to 3 seconds.
timer1.Interval =3000;
//Enable the timer.
timer1.Enabled =true;
//Append the text to the sample file.
StreamWriter writer =File.AppendText(@"C:\sample.txt");
writer.WriteLine("Start");
writer.Close();
8.
以下代码添加到 Service 1 类的 OnStop 过程。
VisualBasic.NET 代码'Open the C:\sample.txt file in append mode.
FileOpen(1, "C:\sample.txt", OpenMode.Append)
'Print the text 'Stop' to the C:\sample.txt file.
Print(1, "Stop")
FileClose()VisualC # .NET 代码//Append the text Stop to the C:\sample.txt file.
StreamWriter writer =File.AppendText(@"C:\sample.txt");
writer.WriteLine("Stop");
writer.Close();
9.
将以下代码添加到 Timer 1 组件的 Tick 事件。
VisualBasic.NET 代码'Set the enabled property to false.
Timer1.Enabled = False
'Open the C:\sample.txt file in append mode.
FileOpen(1, "C:\sample.txt", OpenMode.Append)
'Print the text 'Tick' to the C:\sample.txt file.
Print(1, "Tick")
FileClose()
'Enable the timer.
Timer1.Enabled = TrueVisualC # .NET 代码//Set the enable property to false.
timer1.Enabled =false;
//Append the text Tick to the C:\sample.txt file.
StreamWriter writer =File.AppendText(@"C:\sample.txt");
writer.WriteLine("Tick");
writer.Close();
timer1.Enabled =true;
10.
在 视图 菜单上, 单击 设计器 。
11.
右击 设计器 , 然后单击 添加 Installer 。
默认情况下, ServiceInstaller 1 和 ServiceProcessInstaller1 创建。
12.
ServiceInstaller 1 , 右击, 然后单击 属性 。
13.
将 DisplayName 属性设置为 Service 1 。
注意最好要将 ServiceName属性设为 Service 1。
14.
右击 ServiceProcessInstaller1 , 然后单击 属性 。
15.
在属性窗口, ServiceProcessInstaller1 将以 LocalSystem 帐户 属性。
16.
在 生成 菜单上, 单击 BuildSolution@@@ 。
17.
开始 , 依次 运行 。
18.
在运行窗口, 在 打开 框中, 键入 cmd , 然后按 ENTER 键。
19.
在命令提示符下, 运行以下命令:installutil WindowsService1.exe注意如果使用任何其他服务具有相同名称不安装您 Service 1服务。
20.
完成安装步骤后, AdministrativeTools , 控制面板中单击, 然后单击 服务 。
21.
在服务窗口, 右击 Service 1 , 依次 开始 。
22.
等待几秒钟, 右击 Service 1 , 然后单击 停止 。
23.
打开 C:\sample.txt 文件。
显示文本 StartStop 。 不引发 Timer 1 Tick 事件, 并且不打印到 C:\sample.txt 文件 Tick 文本。