本文將告訴您,如何建立安裝專案 (先前稱為 「 NT 服務 」) 的 Windows 服務應用程式。若要執行此動作您必須先建立包含簡單的 Windows 服務的方案的項目寫入其應用程式記錄檔的專案。然後,將安裝專案加入至方案,以安裝視窗服務。最後,您啟動服務。您可以使用 [開始] 功能表中的 [系統管理工具] 資料夾中的 [服務] 項目來啟動該服務。
需求
下列清單列出建議的硬體、 軟體、 網路基礎結構和您必須擁有 Service Pack:
- Microsoft Windows 2000、 Windows XP、 Windows Server 2003 或更新版本 Microsoft 作業系統與.NET Framework 安裝
- Microsoft Visual Studio.NET 企業、 Microsoft Visual Studio.NET 企業架構設計人員、 Microsoft Visual Studio 2005 或 Microsoft Visual Studio 2008。
本文假設您已熟悉 Windows 服務。如果您不熟悉 Windows 服務請參閱本文 <
REFERENCES > 一節中第一個的參考]。
本文也假設您使用安裝並執行這項服務的使用者帳戶有權限,您必須安裝,並且啟動服務,而也有權限,您必須要有存取事件記錄檔。
建立 Windows 服務的安裝專案
本節說明如何建立 Windows 服務專案,以及如何使用已編譯的安裝專案來安裝 Windows 服務。
建立 Windows 服務專案
- 啟動 Microsoft Visual Studio。
- 在 [檔案] 功能表上指向 [新增],然後按一下 [專案]。
- 在 [專案類型 下, 按一下 [Visual C# 專案,然後按一下 [範本] 下方的 [Windows 服務。
附註在 Visual Studio 2005] 或 [Visual Studio 2008 展開 Visual C#專案類型] 下,按一下 Windows,然後再按一下 [範本] 下方的 [Windows 服務]。 - 型別 LogWriterService 個 名稱] 文字方塊,然後再鍵入 C:\ 在 [位置] 文字方塊中。按一下 [確定]。
- 在 [方案總管] 中 Service1.cs,] 上按一下滑鼠右鍵,然後按一下 [檢視程式碼。
- 在 OnStart 事件處理常式以下列程式碼取代註解:
EventLog.WriteEntry("My simple service started."); 。 - 在 [方案總管中連按兩下 Service1.cs]。
- 在 [程式碼編輯器] 視窗中以滑鼠右鍵按一下 [設計檢視],然後按一下 [內容
- 在 [屬性] 窗格中按一下 [加入安裝程式] 連結。
- 在針對 ServiceInstaller1 的 [屬性] 窗格中變更 ServiceName 屬性設定為 Service1。
- 在設計檢視] 中的 [程式碼編輯器] 視窗中按一下 [ServiceProcessInstaller1]。
- 在 [屬性] 窗格中將 [帳戶] 屬性的內容變更為 LocalSystem (的 LocalService 和 NetworkService 的值是只能在 Microsoft Windows XP 中使用)。
使用已編譯的安裝專案來安裝 Windows 服務
完成前一節,若要設定 Windows 服務專案步驟之後,請依照下列步驟將封裝服務應用程式,以便在安裝服務應用程式的部署專案加入:
- 將新專案加入至 LogWriterService 專案中。要這麼做,請您執行下列步驟:
- 在方案總管中以滑鼠右鍵按一下 方案 'LogWriterService' (1 專案),指向 [新增],然後再按一下 [新增專案。
- 按一下 [安裝和部署專案 在 [專案類型,然後按一下 [範本] 下的 [安裝專案。
- 在 [名稱] 文字方塊中,輸入 ServiceSetup。
- 型別在 位置 文字 C:\ 方塊,然後再按一下 [確定]。
- 請告訴部署專案時要怎麼封裝。要這麼做,請您執行下列步驟:
- 在 [方案總管] 中 ServiceSetup] 上按一下滑鼠右鍵,指向 [新增],然後再按一下 [專案輸出
- 在 [加入專案輸出群組] 對話方塊的 [專案] 方塊中按一下 [LogWriterService
- 按一下 [主要輸出],然後再按一下 [確定]。
- 為了正確安裝新增只有主要輸出。若要加入自訂動作,請依照下列步驟執行:
- 在 [方案總管] 中 ServiceSetup] 上按一下滑鼠右鍵,指向 [檢視],然後再按一下 [自訂動作
- 自訂動作,] 上按一下滑鼠右鍵,然後按一下 [加入自訂動作。
- 按一下 [應用程式資料夾,然後按一下 [確定]。
- 按一下 [LogWriterService 來自主要輸出 (Active),然後按一下 [確定]。
請注意 主要輸出 會出現在 [安裝]、 認可、 復原 和 解除安裝。
- 預設情況下,安裝專案並未包含在組建組態中。若要建置方案,使用下列方法之一:
- 方法 1
- LogWriterService,] 上按一下滑鼠右鍵,然後按一下 [建置]。
- 以滑鼠右鍵按一下 ServiceSetup,然後按一下 [建立。
- 方法 2
- 在 建置] 功能表上按一下 [組態管理員] 來建置整個方案]。
- 按一下以選取 ServiceSetup [建置] 核取方塊。
- 請按 F7 鍵以建置整個方案。建置方案時您會有適用於該服務的完整安裝封裝。
- 若要安裝新建置的服務,ServiceSetup,] 上按一下滑鼠右鍵,然後按一下 [安裝]。
- 在 [ServiceSetup] 對話方塊按三次 [下一步]。請注意,服務安裝時,會出現一個進度列。
- 安裝服務時按一下 [關閉]。
完成程式碼清單
Service1.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
namespace LogWriterService
{
public class Service1 : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Service1()
{
// The Windows.Forms Component Designer must have this call.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user service may run in the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = New System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = "Service1";
}
/// <summary>
/// Clean up any resources that are being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
EventLog.WriteEntry("My simple service started.");
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
}
}
}請確認它可以運作
- 在控制台中按兩下 [系統管理工具,然後再按兩下 [服務
- Service1 上, 按一下滑鼠右鍵,然後按一下 [開始]
- 使用下列方法之一來驗證的事件記錄在事件記錄檔:
- 方法 1
- 控制台中按兩下 [系統管理工具,然後再按兩下 [事件檢視器。
- 在左窗格中按一下 應用程式記錄檔,然後再找出事件記錄檔,為您的服務在右邊窗格中。
- 方法 2
- 在 [伺服器總管] 中展開 [伺服器]、 展開 [ComputerName、 展開 [事件記錄檔、 展開 [應用程式,然後再展開 Service1]。 回想一下 Service1 為非服務類別的名稱本身。因此,Service1 作為應用程式名稱。 (它是以說明如何自訂名稱本文的討論範圍內)。
- 記錄檔項目上移動游標。第二個項目從頂端應該閱讀我簡單服務啟動 」。
疑難排解
Framework SDK 文件中聲明下列:
服務應用程式專案會建立已編譯可執行檔必須先安裝在伺服器上專案能以有意義的方式運作。您無法偵錯,或按下 F5 或 F11 執行服務應用程式 ; 您不能立即執行服務或步驟至它的程式碼。而是,您必須安裝和啟動您的服務並再將偵錯工具附加至服務的處理程序。
如需詳細資訊請造訪下列 Microsoft 開發 o 人 h 員 ? 工 u 具 ? 網路 (MSDN) 網站]:
若要建立有用的 Windows 服務,會示範如何安裝、 如何測試,以及如何偵錯服務,請造訪下列 MSDN 網站:
如需詳細資訊請參閱從 2001 年十二月問題 MSDN Magazine 的下列文件: