1.首先添加一個windows服務程序
2.在 protected override void OnStart(string[] args)中加入我們的業務邏輯代碼
3.在我們的GateService中的設計頁面,添加安裝程序
4.配置安裝程序serviceProcessInstaller1和serviceInstaller1
serviceInstaller1中 StartType設置為Manual,ServiceName設置為我們的服務名
serviceProcessInstaller1 中設置服務類型,Account設置為LocalService
---------------------以上我們的服務就開發完成了,但是服務不能直接啟動,我們需要一個應用程序來啟動我們的服務
1.建立一個winform應用程序來啟動服務
2.ui頁面設計四個按鈕:安裝、啟動、停止、卸載
3.對我們的服務進行引用
4.winform代碼:
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.Data; using System.Drawing; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace GateServiceClientManager { public partial class FrmClient : Form { string serviceFilePath = $"{Application.StartupPath}\\GateServiceManager.exe";//這里是我們服務的exe string serviceName = "GateService"; public FrmClient() { InitializeComponent(); } private void btnset_Click(object sender, EventArgs e) { this.BeginInvoke(new Action(() => { if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName); this.InstallService(serviceFilePath); MessageBox.Show("服務安裝成功!"); })); } private void btnstart_Click(object sender, EventArgs e) { this.BeginInvoke(new Action(() => { if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName); MessageBox.Show("服務啟動成功!"); })); } private void btnstop_Click(object sender, EventArgs e) { this.BeginInvoke(new Action(() => { if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName); MessageBox.Show("服務停止成功!"); })); } private void btnunset_Click(object sender, EventArgs e) { this.BeginInvoke(new Action(() => { if (this.IsServiceExisted(serviceName)) { this.ServiceStop(serviceName); this.UninstallService(serviceFilePath); MessageBox.Show("服務卸載成功!"); } })); } //判斷服務是否存在 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(); } } } } }
以上這樣我們就可以啟動winform程序來打開我們的服務啦
遇到的問題:
1.如果需要在GateServiceManager(也就是我們的服務)加入配置文件的信息
比如:
在服務中的app.config是讀取不到的,在winform中加也是讀取不到的。
解決辦法:我們需要在服務中心加入app.config,然后再把服務的app.config復制到winform程序中這樣才可以讀取到我們的配置信息
2.如果用winform程序開啟服務時,出現:其他信息: 無法啟動計算機“.”上的服務 zjService。那一定是你的服務代碼寫的有問題。不用懷疑直接去改就好了
--------------------關於服務調試
首先我們要把我們的服務啟動
2.用vs把服務添加到進程中,進行調試
找到我們的服務,然后就可以調試了
注意:在調試OnStart時,一定要加一個延遲Thread.Sleep(30000); ,要不然總是調試不到。真的有點坑!!!
至此,你就掌握了如何開發windows服務啦~