溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#Windows服務(wù)程序中如何為Windows服務(wù)創(chuàng)建安裝項目

發(fā)布時間:2021-11-18 10:47:36 來源:億速云 閱讀:209 作者:小新 欄目:編程語言

小編給大家分享一下C#Windows服務(wù)程序中如何為Windows服務(wù)創(chuàng)建安裝項目,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

C#Windows服務(wù)程序之安裝項目的由來:本文介紹如何創(chuàng)建Windows 服務(wù)應(yīng)用程序(以前稱為"NT 服務(wù)")的一個安裝項目。 為此,必須首先創(chuàng)建一個解決方案包含簡單的 Windows 服務(wù)項目,將項寫入它的應(yīng)用程序日志。 然后您將一個安裝程序項目添加到解決方案以安裝 Window 服務(wù)。 ***,您啟動該服務(wù)。 您可以通過使用開始菜單中的管理工具文件夾中的服務(wù)項啟動服務(wù)。

C#Windows服務(wù)程序之為Windows 服務(wù)創(chuàng)建安裝項目

此部分介紹了如何創(chuàng)建 Windows 服務(wù)項目,以及如何使用一個已編譯的安裝項目若要安裝 Windows 服務(wù)。

C#Windows服務(wù)程序之創(chuàng)建一個 Windows 服務(wù)項目

1、啟動Microsoft Visual Studio。

2、在 文件 菜單上, 指向 新建 ,然后單擊 Project 。

3、在 項目類型 ,下單擊 Visual C# 項目 ,然后在 模板 下單擊 Windows 服務(wù) 。

注意 在 Visual Studio 2005 或 Visual Studio 2008,展開 Visual C# 項目類型 下,單擊 Windows ,然后單擊 Windows 服務(wù) 在 模板 下。

4、類型 LogWriterService 名稱 文本框中,然后鍵入 C:\ 在 位置 文本框中中。 單擊 確定 。

5、在解決方案資源管理器, Service1.cs ,右鍵單擊,然后單擊 查看代碼 。

6、在 OnStart 事件處理程序中, 替換注釋以下代碼:

EventLog.WriteEntry("My simple service started.");

7、在解決方案資源管理器,雙擊 Service1.cs 。

8、在代碼編輯器窗口,用鼠標(biāo)右鍵單擊 設(shè)計視圖 ,然后單擊 屬性

9、在屬性窗格中, 單擊 添加安裝程序 鏈接。

10、在為 ServiceInstaller 1 屬性窗格,更改 ServiceName 屬性,以 Service 1 。

11、在設(shè)計視圖中代碼編輯器窗口,單擊 ServiceProcessInstaller 1 。

12、在屬性窗格,將 帳戶 屬性更改為 LocalSystem (: LocalService 和 NetworkService 值都可僅在 Microsoft Windows XP 中獲得)。

使用一個已編譯的安裝程序項目來安裝 Windows 服務(wù)

在完成上一節(jié)來配置 Windows 服務(wù)項目中的步驟之后,請按照下列步驟添加部署項目打包服務(wù)應(yīng)用程序以便可以安裝服務(wù)應(yīng)用程序的操作:

1、將一個新的項目添加到您的 LogWriterService 項目中。 為此,請按照下列步驟操作:

a、在解決方案資源管理器,右鍵單擊 解決方案 LogWriterService (1 項目) ,指向 添加 ,然后單擊 新建項目 。

b、單擊 安裝和部署項目 在 項目類型 ,然后在 模板 下單擊 安裝程序項目 。

c、在 名稱 文本框中,鍵入 ServiceSetup 。

d、類型 C:\ 位置 文本中框,然后再單擊 確定 。

2、告訴在部署項目的內(nèi)容到程序包。 為此,請按照下列步驟操作:

a、在解決方案資源管理器,右鍵單擊 ServiceSetup ,指向 添加 ,然后單擊 Project Output (項目輸出

b、在 添加項目輸出組 對話框, 項目 框中的單擊 LogWriterService

c、單擊 Primary output (主要輸出) ,然后單擊 確定 。

3、為正確的安裝,添加僅主輸出。 要添加自定義操作,請按照下列步驟操作:

a、在解決方案資源管理器,右鍵單擊 ServiceSetup ,指向 視圖 ,然后單擊 自定義操作

b、用鼠標(biāo)右鍵單擊 自定義操作 ,然后單擊 添加自定義操作 。

c、單擊 主輸出 LogWriterService (Active) ,然后單擊 確定 。

您會注意到 主輸出 出現(xiàn)在 安裝 、 提交 、 回滾 和 卸載 。

4、默認情況下生成配置中不包含安裝程序項目。 為構(gòu)建C#Windows服務(wù)程序解決方案,使用下列方法之一:

C#Windows服務(wù)程序方法 1

用鼠標(biāo)右鍵單擊 LogWriterService ,然后單擊 生成 。

用鼠標(biāo)右鍵單擊 ServiceSetup ,然后單擊 生成 。

C#Windows服務(wù)程序方法 2

在 生成 菜單上, 單擊 配置管理器 來構(gòu)建整個解決方案。

單擊以選中 生成 復(fù)選框為 ServiceSetup。

按 F 7 鍵來構(gòu)建整個解決方案。 生成解決方案時, 必須可用于該服務(wù)是完整的安裝包。

5、若要安裝新建的服務(wù), ServiceSetup ,右鍵單擊,然后單擊 安裝 。

6、在 ServiceSetup 對話框,單擊 下一步 三次。 您會注意到一個進度欄出現(xiàn)服務(wù)安裝期間。

7、安裝服務(wù)時, 單擊 關(guān)閉 。

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.  }  }  }

C#Windows服務(wù)程序驗證它正常工作

1、在控制面板,雙擊 管理工具 ,然后雙擊 服務(wù)

2、用鼠標(biāo)右鍵單擊 Service 1 ,然后單擊 開始

3、使用以下方法之一來驗證事件日志中記錄一個事件:

C#Windows服務(wù)程序驗證方法 1

a、在控制面板中, 雙擊 管理工具 ,然后雙擊 事件查看器 。

b、在左窗格中, 單擊 應(yīng)用程序日志 ,然后查找從右窗格中您的服務(wù)在事件日志中。

C#Windows服務(wù)程序驗證方法 2

a、在 Server Explorer (服務(wù)器資源管理器,) 中展開 服務(wù)器 、 ComputerName、 事件日志 、 展開 應(yīng)用程序 ,然后展開 Service 1 。 請記住 Service 1 是類,非服務(wù)的名稱本身。 因此, Service 1 用作應(yīng)用程序的名稱。 (它是超出了本文說明了如何自定義名稱的范圍。

b、日志條目上移動光標(biāo)。 從頂部第二個條目應(yīng)閱讀"我的簡單服務(wù)開始"。

看完了這篇文章,相信你對“C#Windows服務(wù)程序中如何為Windows服務(wù)創(chuàng)建安裝項目”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI