用.NET創(chuàng)建Windows服務(wù)的方法第2/2頁(yè)
安裝Windows服務(wù)
Windows服務(wù)不同于普通Windows應(yīng)用程序。不可能簡(jiǎn)簡(jiǎn)單單地通過(guò)運(yùn)行一個(gè)EXE就啟動(dòng)Windows服務(wù)了。安裝一個(gè)Windows服務(wù)應(yīng)該通過(guò)使用.NET Framework提供的InstallUtil.exe來(lái)完成,或者通過(guò)諸如一個(gè)Microsoft Installer (MSI)這樣的文件部署項(xiàng)目完成。
添加服務(wù)安裝程序
創(chuàng)建一個(gè)Windows服務(wù),僅用InstallUtil程序去安裝這個(gè)服務(wù)是不夠的。你必須還要把一個(gè)服務(wù)安裝程序添加到你的Windows服務(wù)當(dāng)中,這樣便于InstallUtil或是任何別的安裝程序知道應(yīng)用你服務(wù)的是怎樣的配置設(shè)置。
1. 將這個(gè)服務(wù)程序切換到設(shè)計(jì)視圖
2. 右擊設(shè)計(jì)視圖選擇“添加安裝程序”
3. 切換到剛被添加的ProjectInstaller的設(shè)計(jì)視圖
4. 設(shè)置serviceInstaller1組件的屬性:
1) ServiceName = My Sample Service
2) StartType = Automatic
5. 設(shè)置serviceProcessInstaller1組件的屬性
1) Account = LocalSystem
6. 生成解決方案
在完成上面的幾個(gè)步驟之后,會(huì)自動(dòng)由Visual Studio產(chǎn)生下面的源代碼,它包含于ProjectInstaller.cs這個(gè)源文件內(nèi)。
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
namespace CodeGuru.MyWindowsService
{
/// <summary>
/// Summary description for ProjectInstaller.
/// </summary>
[RunInstaller(true)]
public class ProjectInstaller :
System.Configuration.Install.Installer
{
private System.ServiceProcess.ServiceProcessInstaller
serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public ProjectInstaller()
{
// This call is required by the Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new
System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new
System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.ServiceName = "My Sample Service";
this.serviceInstaller1.StartType =
System.ServiceProcess.ServiceStartMode.Automatic;
//
// ProjectInstaller
//
this.Installers.AddRange(new
System.Configuration.Install.Installer[]
{this.serviceProcessInstaller1, this.serviceInstaller1});
}
#endregion
}
}
用InstallUtil安裝Windows服務(wù)
現(xiàn)在這個(gè)服務(wù)已經(jīng)生成,你需要把它安裝好才能使用。下面操作會(huì)指導(dǎo)你安裝你的新服務(wù)。
1. 打開(kāi)Visual Studio .NET命令提示
2. 改變路徑到你項(xiàng)目所在的bin\Debug文件夾位置(如果你以Release模式編譯則在bin\Release文件夾)
3. 執(zhí)行命令“InstallUtil.exe MyWindowsService.exe”注冊(cè)這個(gè)服務(wù),使它建立一個(gè)合適的注冊(cè)項(xiàng)。
4. 右擊桌面上“我的電腦”,選擇“管理”就可以打計(jì)算機(jī)管理控制臺(tái)
5. 在“服務(wù)和應(yīng)用程序”里面的“服務(wù)”部分里,你可以發(fā)現(xiàn)你的Windows服務(wù)已經(jīng)包含在服務(wù)列表當(dāng)中了
6. 右擊你的服務(wù)選擇啟動(dòng)就可以啟動(dòng)你的服務(wù)了
在每次需要修改Windows服務(wù)時(shí),這就會(huì)要求你卸載和重新安裝這個(gè)服務(wù)。不過(guò)要注意在卸載這個(gè)服務(wù)前,最好確保服務(wù)管理控制臺(tái)已經(jīng)關(guān)閉,這會(huì)是一個(gè)很好的習(xí)慣。如果沒(méi)有這樣操作的話,你可能在卸載和重安裝Windows服務(wù)時(shí)會(huì)遇到麻煩。僅卸載服務(wù)的話,可以執(zhí)行相的InstallUtil命令用于注銷服務(wù),不過(guò)要在后面加一個(gè)/u命令開(kāi)關(guān)。
調(diào)試Windows服務(wù)
從另外的角度度看,調(diào)試Windows服務(wù)絕不同于一個(gè)普通的應(yīng)用程序。調(diào)試Windows服務(wù)要求的步驟更多。服務(wù)不能象你對(duì)普通應(yīng)用程序做的那樣,只要簡(jiǎn)單地在開(kāi)發(fā)環(huán)境下執(zhí)行就可以調(diào)試了。服務(wù)必須首先被安裝和啟動(dòng),這一點(diǎn)在前面部分我們已經(jīng)做到了。為了便于跟蹤調(diào)試代碼,一旦服務(wù)被啟動(dòng),你就要用Visual Studio把運(yùn)行的進(jìn)程附加進(jìn)來(lái)(attach)。記住,對(duì)你的Windows服務(wù)做的任何修改都要對(duì)這個(gè)服務(wù)進(jìn)行卸載和重安裝。
附加正在運(yùn)行的Windows服務(wù)
為了調(diào)試程序,有些附加Windows服務(wù)的操作說(shuō)明。這些操作假定你已經(jīng)安裝了這個(gè)Windows服務(wù)并且它正在運(yùn)行。
1. 用Visual Studio裝載這個(gè)項(xiàng)目
2. 點(diǎn)擊“調(diào)試”菜單
3. 點(diǎn)擊“進(jìn)程”菜單
4. 確保 顯示系統(tǒng)進(jìn)程 被選
5. 在 可用進(jìn)程 列表中,把進(jìn)程定位于你的可執(zhí)行文件名稱上點(diǎn)擊選中它
6. 點(diǎn)擊 附加 按鈕
7. 點(diǎn)擊 確定
8. 點(diǎn)擊 關(guān)閉
9. 在timer1_Elapsed方法里設(shè)置一個(gè)斷點(diǎn),然后等它執(zhí)行
總結(jié)
現(xiàn)在你應(yīng)該對(duì)Windows服務(wù)是什么,以及如何創(chuàng)建、安裝和調(diào)試它們有一個(gè)粗略的認(rèn)識(shí)了。Windows服務(wù)的額處的功能你可以自行研究。這些功能包括暫停(OnPause)和恢復(fù)(OnContinue)的能力。暫停和恢復(fù)的能力在默認(rèn)情況下沒(méi)有被啟用,要通過(guò)Windows服務(wù)屬性來(lái)設(shè)置。
About the Author
Mark Strawmyer, MCSD, MCSE (NT4/W2K), MCDBA is a Senior Architect of .NET applications for large and mid-size organizations. Mark is a technology leader with Crowe Chizek in Indianapolis, Indiana. He specializes in architecture, design and development of Microsoft-based solutions. You can reach Mark at mstrawmyer@crowechizek.com.
相關(guān)文章
Entity?Framework使用ObjectContext類
這篇文章介紹了Entity?Framework使用ObjectContext類的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06unity實(shí)現(xiàn)文字滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)文字滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02C# 實(shí)現(xiàn)Trim方法去除字符串前后的所有空格
這篇文章主要介紹了C# 實(shí)現(xiàn)Trim方法去除字符串前后的所有空格,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12unity scrollRect實(shí)現(xiàn)按頁(yè)碼翻頁(yè)效果
這篇文章主要為大家詳細(xì)介紹了unity scrollRect實(shí)現(xiàn)按頁(yè)碼翻頁(yè)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-04-04C#中comboBox實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)
給大家分享了C#中comboBox實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)的全部代碼,代碼經(jīng)過(guò)測(cè)試,有興趣的朋友跟著做一下。2018-03-03C#中StringBuilder用法以及和String的區(qū)別分析
當(dāng)我們?cè)诔鯇W(xué)使用C#時(shí),常常會(huì)不知道該用StringBuilder合適還是用String高效,下面是我在學(xué)習(xí)當(dāng)中對(duì)StringBuilder和String的區(qū)別總結(jié),分享給大家。2013-03-03C#對(duì)DataTable中的某列進(jìn)行分組
這篇文章介紹了C#對(duì)DataTable某列進(jìn)行分組的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03