冒着陈述显而易见的风险,如果您有任何 C/C++/Java 背景,我认为 C# 为您提供了最低的切入点。
假设您使用的是 Visual Studio 2008,您可以按照以下步骤操作:
- 打开 Visual Studio 2008,然后选择“文件”|“新建”|“项目”菜单选项。
- In the New Project dialog...
- 在项目类型中选择 Visual C#|Windows 节点
- 选择Windows服务模板
- 输入项目的名称和位置
- Press OK
- 此时,您已经掌握了 Windows 服务的所有基础知识。 Program.cs 文件包含服务的 Main() 方法,Service1.cs 定义作为新 Windows 服务的 System.ServiceProcess.ServiceBase 组件。
- In the Property Grid for your Service1 component, consider setting the following properties at a minimum:
- (名称)- 为您的对象指定一个直观的名称,例如 ServiceExample
- 自动记录 - 设置为
false
防止事件默认写入应用程序事件日志(注意:我并不是说您不应该记录服务事件;我只是更喜欢写入我自己的事件日志而不是应用程序日志 - 见下文)
- CanShutdown - 设置为
true
如果你想处理系统关闭
- ServiceName - 定义服务控制管理器 (SCM) 识别您的服务的名称
- 在 ServiceExample 的代码中,OnStart() 和 OnStop() 虚拟函数被删除。您需要用您的服务显然需要执行的任何操作来填写这些内容。如果您将 CanShutdown 属性更改为
true
,您还需要重写 OnShutdown 方法。我在下面创建了一个示例来说明这些函数的用法。
- 至此,ServiceExample 服务基本完成,但是您还需要一种方法来安装它。为此,请在设计器中打开 ServiceExample 组件。右键单击设计器面板中的任意位置,然后选择“添加安装程序”菜单选项。这会向您的项目添加一个 ProjectInstaller 组件,其中包含两个附加组件 - serviceProcessInstaller1 和 serviceInstaller1。
- Select the serviceProcessInstaller1 component in the designer. In the Property Grid, consider setting the following properties:
- (名称)- 为您的对象指定一个直观的名称,例如 serviceProcessInstaller
- 帐户 - 至少选择 LocalService 帐户,但如果您的服务需要更多权限,则可能必须使用 NetworkService 或 LocalSystem 帐户
- Select the serviceInstaller1 component in the designer. In the Property Grid, consider setting the following properties:
- (名称) - 给你的对象一个直观的名称,例如 serviceInstaller
- 描述 - 服务的描述将显示在您的服务的 SCM 中
- DisplayName - 您的服务的友好名称,它将显示在您的服务的 SCM 中
- ServiceName - 确保该名称与您为 ServiceExample 组件的 ServiceName 属性选择的名称相同(请参阅步骤 4)
- StartType - 指示您希望服务自动启动还是手动启动
- 请记住,我说过我更喜欢将事件写入我自己的事件日志而不是应用程序事件日志。为此,您需要将 ProjectInstaller 中的默认 EventLogInstaller 替换为自定义 EventLogInstaller。让 ProjectInstaller 的代码如下所示:
using System.Diagnostics;
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
EventLogInstaller installer = FindInstaller(this.Installers);
if (installer != null)
{
installer.Log = "ServiceExample"; // enter your event log name here
}
}
private EventLogInstaller FindInstaller(InstallerCollection installers)
{
foreach (Installer installer in installers)
{
if (installer is EventLogInstaller)
{
return (EventLogInstaller)installer;
}
EventLogInstaller eventLogInstaller = FindInstaller(installer.Installers);
if (eventLogInstaller != null)
{
return eventLogInstaller;
}
}
return null;
}
}
此时,您可以构建项目来获取 Windows 服务可执行文件。要安装服务,请打开 Visual Studio 2008 命令提示符,然后导航到可执行文件所在的“调试”或“发布”目录。在命令提示符处,键入以下内容:InstallUtil ServiceExample.exe。这将在本地计算机上安装您的服务。要卸载它,请在命令提示符处键入以下命令:InstallUtil /u ServiceExample.exe
只要您的服务未运行,您就可以对服务进行更改并重新构建,即,您不必卸载服务即可对其进行更改。但是,只要可执行文件正在运行,您就无法使用修复和增强功能覆盖该可执行文件。
要查看正在运行的服务,请打开 ServiceExample.cs 文件并进行以下更改:
using System.Diagnostics;
public partial class ServiceExample : ServiceBase
{
public ServiceExample()
{
// Uncomment this line to debug the service.
//Debugger.Break();
InitializeComponent();
// Ties the EventLog member of the ServiceBase base class to the
// ServiceExample event log created when the service was installed.
EventLog.Log = "ServiceExample";
}
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("The service was started successfully.", EventLogEntryType.Information);
}
protected override void OnStop()
{
EventLog.WriteEntry("The service was stopped successfully.", EventLogEntryType.Information);
}
protected override void OnShutdown()
{
EventLog.WriteEntry("The service was shutdown successfully", EventLogEntryType.Information);
}
}
使用这些更改运行服务后,您可以在事件查看器中查看 ServiceExample 事件日志并查看其中记录的消息。
编辑:如果您更喜欢使用应用程序事件日志而不是自定义事件日志来记录事件,只需对 ProjectInstaller.cs 文件不进行任何更改即可。此外,请省略 ServiceExample 构造函数中设置 EventLog 的 Log 属性的行。当您运行服务时,您的日志消息将显示在应用程序事件日志中。