C#操作Windows服務(wù)類System.ServiceProcess.ServiceBase
一、Windows服務(wù)
1、Windows服務(wù)應(yīng)用程序是一種需要長期運行的應(yīng)用程序,它適合服務(wù)器環(huán)境。
2、無用戶界面,任何消息都會寫進Windows事件日志。
3、隨計算機啟動而啟動,不需要用戶一定登錄Windows。
4、通過服務(wù)控制管理器,可以終止、暫停及當需要時啟動Windows服務(wù)。
二、體系結(jié)構(gòu)
System.ServiceProcess命令空間
1、類繼承關(guān)系:
- Object
- Component
- ServiceBase
- ServiceController
- Installer
- ComponentInstaller
- ServiceInstaller
- ServiceProcessInstaller
- Component
2、體系結(jié)構(gòu)
第一部分就是服務(wù)程序。實現(xiàn)系統(tǒng)的業(yè)務(wù)需求。
Service Control Manager(SCM)。SCM是操作系統(tǒng)的一個組成部分(services.exe),作用是于服務(wù)進行通信。
SCM包含一個儲存著已安裝的服務(wù)和驅(qū)動程序的信息的數(shù)據(jù)庫,通過SCM可以統(tǒng)一的、安全的管理這些信息。
一個服務(wù)擁有能從SCM收到信號和命令所必需的的特殊代碼,并且能夠在處理后將它的狀態(tài)回傳給SCM。
ServiceBase:(服務(wù)程序)實現(xiàn)系統(tǒng)的業(yè)務(wù)需求。 在創(chuàng)建新的服務(wù)類時,必須從 ServiceBase 派生。
第二部分服務(wù)控制程序,是一個Service Control Dispatcher(SCP)。
它是一個擁有用戶界面,允許用戶開始、停止、暫停、繼續(xù),并且控制一個或多個安裝在計算機上服務(wù)的Win32應(yīng)用程序。
SCP的作用是與SCM通訊,Windows 管理工具中的“服務(wù)”就是一個典型的SCP。
ServiceController:(服務(wù)控制程序)表示 Windows 服務(wù)并允許連接到正在運行或者已停止的服務(wù)、對其進行操作或獲取有關(guān)它的信息。
第三部分、服務(wù)配置程序配置程序可以安裝服務(wù),向注冊表注冊服務(wù),設(shè)置服務(wù)的啟動類型、服務(wù)的用戶及依存關(guān)系等。
ServiceInstaller:(服務(wù)安裝配置程序)繼承自Installer類。該類擴展 ServiceBase 來實現(xiàn)服務(wù)。 在安裝服務(wù)應(yīng)用程序時由安裝實用工具調(diào)用該類。
ServiceProcessInstaller :(服務(wù)安裝配置程序)繼承自Installer類。安裝一個可執(zhí)行文件,該文件包含擴展 ServiceBase 的類。 該類由安裝實用工具(如 InstallUtil.exe)在安裝服務(wù)應(yīng)用程序時調(diào)用。
三、創(chuàng)建Windows服務(wù):ServiceBase
新建一個“Windows服務(wù)”項目,添加一個System.Timers.Timer組件。
1)單個服務(wù)
static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new MyService1() }; ServiceBase.Run(ServicesToRun); }
服務(wù)程序:
public partial class MyService1 : ServiceBase { public MyService1() { InitializeComponent(); myTimer = new System.Timers.Timer(); myTimer.Interval = 60000; //設(shè)置計時器事件間隔執(zhí)行時間 myTimer.Elapsed += (timer1_Elapsed); this.ServiceName = "我的服務(wù)"; this.AutoLog = true;//是否自行寫入系統(tǒng)的事件日志 this.CanHandlePowerEvent = true;//是否接受電源事件 this.CanPauseAndContinue = true;//是否能暫?;蚶^續(xù) this.CanShutdown = true;//是否在計算機關(guān)閉時收到通知 this.CanStop = true;//是否接受停止運行的請求 } private void timer1_Elapsed(object sender, ElapsedEventArgs e) { File.AppendAllText("C:\\1.txt", "Service Runing"); } string filePath = @"D:\MyServiceLog.txt"; protected override void OnStart(string[] args) { this.timer1.Enabled = true; File.AppendAllText("C:\\1.txt", "Service Started"); } protected override void OnStop() { this.timer1.Enabled = false; File.AppendAllText("C:\\1.txt", "Service Stoped"); } }
服務(wù)在運行時,獲取其可執(zhí)行文件的父目錄:
AppDomain.CurrentDomain.BaseDirectory;
2)多個服務(wù)
static void Main() { ServiceBase[] ServicesToRun; string Line = Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory).Name; DCWinService lineService = new DCWinService(Line); lineService.ServiceName = "GPE.PAMSDC.DCService(" + Line + ")"; ServicesToRun = new ServiceBase[] { lineService }; ServiceBase.Run(ServicesToRun); }
服務(wù)程序:
public partial class DCWinService : ServiceBase { public DCWinService() { InitializeComponent(); } string line; public DCWinService(string line) { this.line = line; } protected override void OnStart(string[] args) { // TODO: 在此處添加代碼以啟動服務(wù)。 GPE.PAMSDC.DCServer.Start(line);//動態(tài)加載 } protected override void OnStop() { // TODO: 在此處添加代碼以執(zhí)行停止服務(wù)所需的關(guān)閉操作。 GPE.PAMSDC.DCServer.Stop(line); } }
四、添加服務(wù)安裝程序:(與服務(wù)程序同一項目)
創(chuàng)建一個Windows服務(wù),僅用InstallUtil程序去安裝這個服務(wù)是不夠的。你必須還要把一個服務(wù)安裝程序添加到你的Windows服務(wù)當中,這樣便于InstallUtil或是任何別的安裝程序知道應(yīng)用你服務(wù)的是怎樣的
配置設(shè)置。
在服務(wù)程序的設(shè)計視圖右擊“添加安裝程序”,自動添加一個ProjectInstaller文件“DCServiceInstaller”。
在ProjectInstaller的設(shè)計視圖中,分別設(shè)置serviceInstaller1組件和serviceProcessInstaller1的屬性。
1)單個服務(wù):
// serviceInstaller1 this.serviceInstaller1.Description = "消息發(fā)送服務(wù)."; this.serviceInstaller1.DisplayName = "MyService1"; this.serviceInstaller1.ServiceName = "MyService1";//要與前面的定義的服務(wù)名一致。 this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic; // serviceProcessInstaller1 this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; this.serviceProcessInstaller1.Password = null; this.serviceProcessInstaller1.Username = null; // DCServiceInstaller this.Installers.AddRange(new System.Configuration.Install.Installer[] { this.serviceInstaller1, this.serviceProcessInstaller1});
2)多個服務(wù):
string[] lines = new string[] { "T1", "T2" }; ServiceInstaller serviceInstaller1; foreach (string line in lines) { // serviceInstaller1 serviceInstaller1 = new ServiceInstaller(); serviceInstaller1.Description = "消息發(fā)送服務(wù)."; serviceInstaller1.DisplayName = "GPE.PAMSDC.DCService(" + line + ")"; serviceInstaller1.ServiceName = "GPE.PAMSDC.DCService(" + line + ")"; this.Installer.Add(this.serviceInstaller1);//serviceInstaller可以有多個 } // serviceProcessInstaller1 this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem; // DCServiceInstaller this.Installers.Add(this.serviceProcessInstaller1);//serviceProcessInstaller只能有一個
注意:在服務(wù)安裝程序中,獲取可執(zhí)行文件的父目錄:
Directory.CreateDirectory("./").Name
五、Windows服務(wù)的安裝程序
1、創(chuàng)建一個“安裝部署”的項目,右鍵項目名稱,選擇“添加”-“項目輸出”,選擇前面創(chuàng)建的服務(wù)項目,再選擇“主輸出”。也可以右擊安裝項目,“視圖”,“添加自定義操作”。
2、使用InstallUtil.exe工具,批處理文件為:
- 安裝:
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe ./GPE.PAMSDC.DCService.exe
Net Start DCService
sc config DCServicestart= auto
- 卸載:
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe -u ./GPE.PAMSDC.DCService.exe
通過第三方組件 (Topshelf)創(chuàng)建C# Windows服務(wù)應(yīng)用程序。
六、服務(wù)控制程序:ServiceController
List<ServiceController> services = new List<ServiceController>(ServiceController.GetServices()); services = services.FindAll(s => s.DisplayName.StartsWith("GPE.PAMSDC.DCService")); services.Sort((s1, s2) => s1.DisplayName.CompareTo(s2.DisplayName)); List<ServiceControllerInfo> serviceInfo = services.ConvertAll(s => new ServiceControllerInfo(s)); foreach (ServiceControllerInfo si in serviceInfo) { if (si.EnableStart) { si.Controller.Start(); si.Controller.WaitForStatus(ServiceControllerStatus.Running); } }
七、調(diào)試Windows服務(wù)
必須首先啟動服務(wù),然后將一個調(diào)試器附加到正在運行的服務(wù)的進程中。
1、用VS加載這個服務(wù)項目。
2、“調(diào)試”菜單,“附加到進程”。
3、確保“顯示所有用戶進行”被選擇。
4、在“可用進程”列表中,選中你的可執(zhí)行文件的名稱。
5、點擊“附加”按鈕。
6、在timer_Elapsed方法中設(shè)置斷點,然后執(zhí)行,從而實現(xiàn)調(diào)試的目的。
到此這篇關(guān)于C#操作Windows服務(wù)類System.ServiceProcess.ServiceBase的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- C#實現(xiàn)Windows服務(wù)測試與調(diào)試
- C#創(chuàng)建Windows服務(wù)與服務(wù)的安裝、卸載
- C#創(chuàng)建控制Windows服務(wù)
- C#創(chuàng)建Windows服務(wù)的實現(xiàn)方法
- C#對Windows服務(wù)組的啟動與停止操作
- C#使用windows服務(wù)開啟應(yīng)用程序的方法
- C#實現(xiàn)操作windows系統(tǒng)服務(wù)(service)的方法
- c#創(chuàng)建windows服務(wù)入門教程實例
- c#創(chuàng)建windows服務(wù)(Windows Services)詳細步驟
- C#編寫Windows服務(wù)實例代碼
相關(guān)文章
C# 9.0新特性——擴展方法GetEnumerator支持foreach循環(huán)
這篇文章主要介紹了C# 9.0新特性——擴展方法GetEnumerator支持foreach循環(huán)的相關(guān)資料,幫助大家更好的理解和學習c# 9.0,感興趣的朋友可以了解下2020-11-11C#中構(gòu)造函數(shù)和析構(gòu)函數(shù)用法實例詳解
這篇文章主要介紹了C#中構(gòu)造函數(shù)和析構(gòu)函數(shù)用法,結(jié)合實例形式詳細分析了C#中構(gòu)造函數(shù)與析構(gòu)函數(shù)的原理、定義、使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-06-06picturebox加載圖片的三種方法與網(wǎng)站驗證碼的抓取
這篇文章主要介紹了picturebox加載圖片的三種方法與網(wǎng)站驗證碼的抓取,需要的朋友可以參考下2015-03-03C#字符串數(shù)組轉(zhuǎn)換為整形數(shù)組的方法
這篇文章主要介紹了C#字符串數(shù)組轉(zhuǎn)換為整形數(shù)組的方法,涉及C#數(shù)組遍歷與轉(zhuǎn)換的相關(guān)技巧,需要的朋友可以參考下2015-06-06C#中LinkedList<T>的存儲結(jié)構(gòu)詳解
這篇文章主要介紹了深度解析C#中LinkedList<T>的存儲結(jié)構(gòu),本文將從鏈表的基礎(chǔ)特性、C#中LinkedList的底層實現(xiàn)邏輯,.NET的不同版本對于Queue的不同實現(xiàn)方式的原因分析等幾個視角進行簡單的解讀,需要的朋友可以參考下2023-12-12