基本参照使用C#建立Windows服务,添加了部份内容html
建立Windows Service
可视化管理Windows Service
调试
示例代码git
选择C#标签的Windows Service项目,并建立
初始结构目录以下
修改Service1为TimingService
双击TimingService.cs,如图所示
点击末尾的switch to code view
进入代码编辑页面github
public partial class TimingService : ServiceBase { public TimingService() { InitializeComponent(); } protected override void OnStart(string[] args) { } protected override void OnStop() { } }
在OnStart和OnStop方法中写上服务启动、中止须要完成的工做
这里咱们写一个计时器,每分钟录入一条日志,在此不赘述,能够看示例代码了解
在设计界面右键菜单栏,点击Add Installer
出现两个组件,serviceProcessInstaller1
和serviceInstaller1
,对它们的属性进行修改c#
ServiceName改成TimingService,是在windows service列表里面的服务名称
Description改成"一个计时器",这里设置的是在windows service列表里面的服务描述
StartType默认为Manual(手动)
相关资料:
ServiceInstaller Classwindows
将Account改成LocalSystem
右键项目点击生成
,在\bin\Debug
目录下生成MyWindowsService.exe
至此,Windows Service建立完毕
相关资料:
ServiceProcessInstaller Classapi
建立一个Windows Form项目MyWindowsService.Client
在窗体内添加四个按钮,分别为安装服务、启动服务、中止服务及卸载服务
引入System.ServiceProcess.dll和System.Configuration.Install.dll,完善相关功能app
//须要引用MyWindowsService项目 string serviceFilePath = $"{Application.StartupPath}\\MyWindowsService.exe"; //这里是设置的serviceName,不是项目名称或者生成的exe的名称 string serviceName = "TimingService"; /// <summary> /// 安装服务 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void InstallButton_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName); this.InstallService(serviceFilePath); } /// <summary> /// 启动服务 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void StartButton_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName); } /// <summary> /// 中止服务 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void StopButton_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName); } /// <summary> /// 卸载服务 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void UninstallButton_Click(object sender, EventArgs e) { if (this.IsServiceExisted(serviceName)) { this.ServiceStop(serviceName); this.UninstallService(serviceFilePath); } } //判断服务是否存在 private bool IsServiceExisted(string serviceName) { ServiceController[] services = ServiceController.GetServices(); foreach (ServiceController sc in services) { if (sc.ServiceName.ToLower() == serviceName.ToLower()) { return true; } } return false; } //安装服务 private void InstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; IDictionary savedState = new Hashtable(); installer.Install(savedState); installer.Commit(savedState); } } //卸载服务 private void UninstallService(string serviceFilePath) { using (AssemblyInstaller installer = new AssemblyInstaller()) { installer.UseNewContext = true; installer.Path = serviceFilePath; installer.Uninstall(null); } } //启动服务 private void ServiceStart(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Stopped) { control.Start(); } } } //中止服务 private void ServiceStop(string serviceName) { using (ServiceController control = new ServiceController(serviceName)) { if (control.Status == ServiceControllerStatus.Running) { control.Stop(); } } }
为了后续管理服务方便,在MyWindowsService.Client引用MyWindowsServiceide
须要使用Administrator的权限才能安装服务
在MyWindowsService.Client项目中右键添加文件应用程序清单
将原来的内容测试
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
改成下面的内容ui
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
以管理员身份启动程序的相关资料:
How do I force my .NET application to run as administrator?
以后从新生成MyWindowsService.Client,在\bin\Debug文件夹下打开MyWindowsService.Client.exe
使用MyWindowsService.Client.exe安装并启动MyWindowsService.exe
能够右键服务点击属性
进行管理
不少种方式,
1.添加一个控制台程序调用业务方法调试
2.打日志调试
3.附加到进程调试
4.官方给的方式,很简单
如何:调试 Windows 服务应用程序
一、三、4适合跟进调试,2适合长期测试并查看问题