C#進階編程:安裝程式

來源:互聯網
上載者:User
切換到Visual Studio .NET的設計檢視,從操作功能表中選擇Add Installer選項,可以給服務添加安裝程式。使用Add Installer選項時,將建立一個新的ProjectInstaller類、一個ServiceInstaller執行個體和一個ServiceProcessInstaller執行個體。圖32-11顯示的是服務的安裝程式類。 圖  32-11根據這張圖表,下面詳細討論由Add Installer選項建立的ProjectInstaller.cs檔案中的原始碼。1. 安裝程式類ProjectInstaller類是從System.Configuration.Install.Installer派生出來的,它是所有定製安裝程式類的基類。使用Installer類,可以建立基於事務的安裝。使用基於事務的安裝時,如果安裝失敗了,系統還可以復原到以前的狀態,安裝程式所做的所有修改都會被取消。32-11所示,Installer類中有Install()、Commit()、Rollback()和Uninstall()方法,這些方法都是從安裝程式中調用的。如果RunInstaller屬性的值為true,則在安裝程式集時就會調用ProjectInstaller類。定製的安裝程式和installutil.exe(這個程式以後將用到)都能檢查該屬性。using System;using System.Collections;using System.ComponentModel;using System.Configuration.Install; namespace Wrox.ProCSharp.WinServices{////// Summary description for ProjectInstaller./// [RunInstaller(true)]public class ProjectInstaller : System.Configuration.Install.Installer{2. ServiceProcessInstaller類和ServiceInstaller類與Windows Forms應用程式類似,InitializeComponent()是在ProjectInstaller類的建構函式中調用的。在InitializeComponent()中,建立了ServiceProcessInstaller類和ServiceInstaller類的執行個體。這兩個類都是從ComponentInstaller類中派生出來的,ComponentInstaller類本身派生於Installer。從ComponentInstaller中派生出來的類可以用作安裝進程的一個部分。記住,一個服務進程可以包括多個服務。ServiceProcessInstaller類用於配置進程,為這個進程中所有服務定義值,而ServiceInstaller類用於服務的配置,因此,每個服務都需要ServiceInstaller類的一個執行個體。如果進程中有3個服務,則必須添加額外的ServiceInstaller對象,也就是說,在這種情況下,需要3個ServiceInstaller執行個體:  private System.ServiceProcess.ServiceProcessInstallerserviceProcessInstaller1; private System.ServiceProcess.ServiceInstaller serviceInstaller1;   ///  ///   Required designer variable.  ///   private System.ComponentModel.Container components = null;  public ProjectInstaller()  {// This call is required by the Designer.InitializeComponent(); // TODO: Add any initialization after the InitComponent call  }  ///  ///   Required method for Designer support - do not modify  ///   the contents of this method with the code editor.  ///  private void InitializeComponent()  {this.serviceProcessInstaller1 =new System.ServiceProcess.ServiceProcessInstaller();this.serviceInstaller1 =new System.ServiceProcess.ServiceInstaller();//// serviceProcessInstaller1//this.serviceProcessInstaller1.Password = null;this.serviceProcessInstaller1.Username = null;//// serviceInstaller1//this.serviceInstaller1.ServiceName = "QuoteService";//// ProjectInstaller//this.Installers.AddRange(  new System.Configuration.Install.Installer[]{this.serviceProcessInstaller1,  this.serviceInstaller1});  }}}ServiceProcessInstaller安裝一個執行ServiceBase類的可執行檔。ServiceProcessInstaller類包含用於整個進程的屬性。在進程中所有服務共用的屬性如表32-1所示。表  32-1
屬   性 描   述
Username,Password 如果Accout屬性設定為ServiceAccout.User,則Username屬性和Password屬性指出服務是在哪一個賬戶下運行
Account 使用這個屬性,可以指定服務的賬戶類型
HelpText HelpText是唯讀屬性,它返回的文本用於協助設定使用者名稱和密碼
用於運行服務的進程可以用ServiceProcessInstaller類的Accout屬性指定,其值可以是ServiceAccout枚舉的任一值,如表32-2所示。表  32-2
意   義
LocalSystem 設定這個值可以指定服務在本地系統上使用有高度許可權的使用者賬戶,但這個賬戶允許匿名使用者進入網路,因此它沒有網路上的許可權
LocalService 這個賬戶類型給任意遠程伺服器提供電腦的認證
NetworkService 類似於LocalService,這個值指定把電腦的認證傳送給遠程伺服器,但與LocalService不同,這種服務可以以非授權使用者的身份登入本地系統。顧名思義,這個賬戶只能用於需要從網路上獲得資源的服務
User 把Accout屬性設定為ServiceAccout.User,表示可以指定應在服務中使用的賬戶
ServiceInstaller是每一個服務都需要的類,這個類的屬性可以用於進程中的每一個服務,其屬性有StartType、DisplayName、ServiceName和ServicesDependedOn,如表32-3所示。表  32-3
屬   性 說   明
StartType StartType 指出服務是手動啟動還是自動啟動。它的值可以是:ServiceStartMode.AutomaticServiceStartMode.ManualServiceStartMode.Disabled。如果使用ServiceStartMode.Disabled,服務就不能啟動。這個選項可用於不應在系統中啟動的服務。例如,如果沒有得到需要的硬體控制器,就可以把該選項設定為Disabled
DisplayName DisplayName 是顯示給使用者的服務的易記名稱。此外,這個名稱也用於控制和監視服務的管理工具  
ServiceName ServiceName是服務的名稱。這個值必須與服務程式中ServiceBase類的ServiceName屬性一致,這個名稱與把ServiceInstaller配置為需要的服務程式相關
ServicesDependentOn 指定必須在服務啟動之前啟動的一個服務組。當服務啟動時,所有相依存的服務都自動啟動,並且我們的服務也將啟動
提示:如果在ServiceBase的衍生類別中改變了服務的名稱,還必須修改ServiceInstaller對象中Service Name屬性的值。注意:在測試階段,最好把StartType的值設定為Manual。如果服務因程式中的錯誤不能停止,仍可以重新啟動系統。如果把StartType的值設定為Automatic,服務就會在重新啟動系統時自動啟動!當確信沒有問題時,可以在以後改變這個配置。3. ServiceInstallerDialog類System.ServiceProcess.Design命名空間中的另一個安裝類是ServiceInstallerDialog。在安裝過程中,如果希望系統管理員輸入使用者名稱和密碼,就可以使用這個類。如果ServiceProcessInstaller類的Account屬性設定為ServiceAccount.User,Username和Password屬性設定為null,則在安裝時,圖32-12所示的Set Service Login對話方塊將自動顯示出來。此時,也可以取消安裝。 圖  32-124. installutil在把安裝類添加到項目中之後,就可以使用installutil.exe公用程式安裝和卸載服務了。這個公用程式可以用於安裝包含Installer類的所有程式集。installutil.exe公用程式調用派生於Installer類的方法Installer()進行安裝,調用UnInstaller()方法進行卸載。安裝和卸載服務的命令分別是:installutil quoteservice.exeinstallutil /u quoteservice.exe注意:如果安裝失敗了,一定要檢查安裝記錄檔InstallUtil.InstallLog和. InstallLog。通常,在安裝記錄檔中可以發現一些非常有用的資訊,例如:“The specified service already exists”。5. 客戶在成功地安裝服務後,就可以從Services MMC中手動啟動服務(詳細內容請參閱32.5節),並啟動客戶應用程式,圖32-13顯示了訪問服務的客戶。 圖  32-13

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.