基本參照使用C#創建Windows服務,添加了部分內容
目錄
概念
創建Windows Service
可視化管理Windows Service
調試
示例代碼
概念
Windows服務是運行在windows后台指定用戶下(默認System)的應用程序,它沒有標准的UI界面
可以設置服務是否與操作系統一起啟動,一起關閉。支持三種方式:1)自動啟動
2)手動啟動
3)禁用
創建Windows Service
選擇C#標簽的Windows Service項目,並創建
初始結構目錄如下
修改Service1為TimingService
雙擊TimingService.cs,如圖所示
點擊末尾的switch to code view
進入代碼編輯頁面
public partial class TimingService : ServiceBase
{
public TimingService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
在OnStart和OnStop方法中寫上服務啟動、停止需要完成的工作
這里我們寫一個計時器,每分鍾錄入一條日志,在此不贅述,可以看示例代碼了解
在設計界面右鍵菜單欄,點擊Add Installer
出現兩個組件,serviceProcessInstaller1
和serviceInstaller1
,對它們的屬性進行修改
serviceInstaller1
ServiceName改為TimingService,是在windows service列表里面的服務名稱
Description改為"一個計時器",這里設置的是在windows service列表里面的服務描述
StartType默認為Manual(手動)
相關資料:
ServiceInstaller Class
serviceProcessInstaller1
將Account改為LocalSystem
右鍵項目點擊生成
,在\bin\Debug
目錄下生成MyWindowsService.exe
至此,Windows Service創建完畢
相關資料:
ServiceProcessInstaller Class
可視化管理Windows Service
創建一個Windows Form項目MyWindowsService.Client
在窗體內添加四個按鈕,分別為安裝服務、啟動服務、停止服務及卸載服務
引入System.ServiceProcess.dll和System.Configuration.Install.dll,完善相關功能
//需要引用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引用MyWindowsService
獲得管理員權限
需要使用Administrator的權限才能安裝服務
在MyWindowsService.Client項目中右鍵添加文件應用程序清單
將原來的內容
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
改為下面的內容
<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 服務應用程序
1、3、4適合跟進調試,2適合長期測試並查看問題