Windows服务

Windows服务也称为Windows Service,它是Windows操作系统和Windows网络的基础,属于系统核心的一部分,它支持着整个Windows的各种操作。诸如DNS客户端、打印程序、Windows更新服务、计划任务、Windows时间服务、告警器等服务,它们关系到机器能否正确运行。如果不能适当地管理这些服务,就会影响到机器的正常操作。

Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序。这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面。这使服务非常适合在服务器上使用,或任何时候,为了不影响在同一台计算机上工作的其他用户,需要长时间运行功能时使用。

本文介绍了如何用C#创建、安装、启动、监控、卸载简单的Windows Service 的内容步骤和注意事项。

一、创建一个Windows Service

1)创建Windows Service项目

C# <wbr>windows服务的创建与调试

2)对Service重命名

将Service1重命名为你服务名称,这里我们命名为ServiceTest。

二、创建服务安装程序

1)添加安装程序

C# <wbr>windows服务的创建与调试

C# <wbr>windows服务的创建与调试
 

 

之后我们可以看到上图,自动为我们创建了ProjectInstaller.cs以及2个安装的组件。

2)修改安装服务名

右键serviceInsraller1,选择属性,将ServiceName的值改为ServiceTest。

 C# <wbr>windows服务的创建与调试
 

3)修改安装权限

右键serviceProcessInsraller1,选择属性,将Account的值改为LocalSystem。

 C# <wbr>windows服务的创建与调试

三、写入服务代码

1)打开ServiceTest代码

右键ServiceTest,选择查看代码。

2)写入Service逻辑

添加如下代码: 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Linq;

using System.ServiceProcess;

using System.Text;

namespace WindowsServiceTest

{

public partial class ServiceTest : ServiceBase

{

public ServiceTest()

{

InitializeComponent();

}

protected override void OnStart(string[] args)

{

using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))

{

sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Start.");

}

}

protected override void OnStop()

{

using (System.IO.StreamWriter sw = new System.IO.StreamWriter("C:\\log.txt", true))

{

sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + "Stop.");

}

}

}

}

这里我们的逻辑很简单,启动服务的时候写个日志,关闭的时候再写个日志。

四、创建安装脚本

打开vs的开发人员命令提示 工具 以管理员身份运行

    

    执行如下所示命令:

   

    ①进入到项目所在文件夹,并进入\bin\Debug\路径下

    ②安装服务命令:installutil ConsoleApplication.exe,然后就可以在服务列表中看到我们启动的服务:

 卸载服务的命令是:installutil /u ConsoleApplication.exe

 当我们修改代码后,需要把服务先卸载,才能重新生成成功

 ③启动服务

 启动服务后,等一会儿再停止服务,然后我们打开D盘下的1.txt文档,可以看到下图所示的结果:

    

4)脚本调试

如果需要查看脚本运行状况,在脚本最后一行加入pause

五、在C#中对服务进行控制

0)配置目录结构

简历一个新WPF项目,叫WindowsServiceTestUI,添加对System.ServiceProcess的引用。

在WindowsServiceTestUI的bin\Debug目录下建立Service目录。

将WindowsServiceTest的生成目录设置为上面创建的Service目录。

生成后目录结构如下图

 C# <wbr>windows服务的创建与调试
 

1)安装

安装时会产生目录问题,所以安装代码如下: 

1

2

3

4

5

6

7

8

string CurrentDirectory = System.Environment.CurrentDirectory;

System.Environment.CurrentDirectory = CurrentDirectory + "\\Service";

Process process = new Process();

process.StartInfo.UseShellExecute = false;

process.StartInfo.FileName = "Install.bat";

process.StartInfo.CreateNoWindow = true;

process.Start();

System.Environment.CurrentDirectory = CurrentDirectory;

2)卸载

卸载时也会产生目录问题,所以卸载代码如下:

1

2

3

4

5

6

7

8

string CurrentDirectory = System.Environment.CurrentDirectory;

System.Environment.CurrentDirectory = CurrentDirectory + "\\Service";

Process process = new Process();

process.StartInfo.UseShellExecute = false;

process.StartInfo.FileName = "Uninstall.bat";

process.StartInfo.CreateNoWindow = true;

process.Start();

System.Environment.CurrentDirectory = CurrentDirectory;

3)启动

代码如下:

1

2

3

4

5

using System.ServiceProcess;

ServiceController serviceController = new ServiceController("ServiceTest");

serviceController.Start();

4)停止

1

2

3

ServiceController serviceController = new ServiceController("ServiceTest");

if (serviceController.CanStop)

serviceController.Stop();

5)暂停/继续

1

2

3

4

5

6

7

8

ServiceController serviceController = new ServiceController("ServiceTest");

if (serviceController.CanPauseAndContinue)

{

if (serviceController.Status == ServiceControllerStatus.Running)

serviceController.Pause();

else if (serviceController.Status == ServiceControllerStatus.Paused)

serviceController.Continue();

}

6)检查状态

1

2

ServiceController serviceController = new ServiceController("ServiceTest");

string Status = serviceController.Status.ToString();

 

六、调试Windows Service

1)安装并运行服务

2)附加进程

C# <wbr>windows服务的创建与调试

C# <wbr>windows服务的创建与调试

 

3)在代码中加入断点进行调试

C# <wbr>windows服务的创建与调试

转载于:https://my.oschina.net/u/3522874/blog/1835559

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值