ServiceInstaller 類別

定義

安裝擴充 ServiceBase 的類別來實作服務, 當安裝服務應用程式時,安裝公用程式會呼叫這個類別。

public ref class ServiceInstaller : System::Configuration::Install::ComponentInstaller
public class ServiceInstaller : System.Configuration.Install.ComponentInstaller
type ServiceInstaller = class
    inherit ComponentInstaller
Public Class ServiceInstaller
Inherits ComponentInstaller
繼承

範例

下列範例會建立名為 MyProjectInstaller 的專案安裝程式,其繼承自 Installer 。 假設有一個服務可執行檔包含兩個服務「Hello-World Service 1」 和 「Hello-World Service 2」。 在安裝公用程式) 所呼叫之 (的建構函 MyProjectInstaller 式中, ServiceInstaller 會為每個服務建立 物件,並針對可執行檔建立 。 ServiceProcessInstaller 若要讓安裝公用程式辨識 MyProjectInstaller 為有效的安裝程式,屬性 RunInstallerAttribute 會設定為 true

在將安裝程式新增至 Installers 集合之前,會在進程安裝程式和服務安裝程式上設定選擇性屬性。 當安裝公用程式存取 MyProjectInstaller 時,會接著安裝透過 呼叫 InstallerCollection.Add 新增至集合的物件 Installers 。 在程式期間,安裝程式會維護狀態資訊,指出已安裝哪些物件,因此,如果發生安裝失敗,就可以輪流備份每個物件。

一般而言,您不會明確建立專案安裝程式類別的實例。 您會建立它並將 屬性新增 RunInstallerAttribute 至語法,但它是實際呼叫的安裝公用程式,因此具現化 類別。

#using <System.dll>
#using <System.ServiceProcess.dll>
#using <System.Configuration.Install.dll>

using namespace System;
using namespace System::Collections;
using namespace System::Configuration::Install;
using namespace System::ServiceProcess;
using namespace System::ComponentModel;

[RunInstaller(true)]
public ref class MyProjectInstaller : public Installer
{
private:
    ServiceInstaller^ serviceInstaller1;
    ServiceInstaller^ serviceInstaller2;
    ServiceProcessInstaller^ processInstaller;

public:
    MyProjectInstaller()
    {
        // Instantiate installers for process and services.
        processInstaller = gcnew ServiceProcessInstaller;
        serviceInstaller1 = gcnew ServiceInstaller;
        serviceInstaller2 = gcnew ServiceInstaller;

        // The services run under the system account.
        processInstaller->Account = ServiceAccount::LocalSystem;

        // The services are started manually.
        serviceInstaller1->StartType = ServiceStartMode::Manual;
        serviceInstaller2->StartType = ServiceStartMode::Manual;

        // ServiceName must equal those on ServiceBase derived classes.
        serviceInstaller1->ServiceName = "Hello-World Service 1";
        serviceInstaller2->ServiceName = "Hello-World Service 2";

        // Add installers to collection. Order is not important.
        Installers->Add( serviceInstaller1 );
        Installers->Add( serviceInstaller2 );
        Installers->Add( processInstaller );
    }

    static void Main()
    {
        Console::WriteLine("Usage: InstallUtil.exe [<service>.exe]");
    }
};

int main()
{
    MyProjectInstaller::Main();
}
using System;
using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;

[RunInstaller(true)]
public class MyProjectInstaller : Installer
{
    private ServiceInstaller serviceInstaller1;
    private ServiceInstaller serviceInstaller2;
    private ServiceProcessInstaller processInstaller;

    public MyProjectInstaller()
    {
        // Instantiate installers for process and services.
        processInstaller = new ServiceProcessInstaller();
        serviceInstaller1 = new ServiceInstaller();
        serviceInstaller2 = new ServiceInstaller();

        // The services run under the system account.
        processInstaller.Account = ServiceAccount.LocalSystem;

        // The services are started manually.
        serviceInstaller1.StartType = ServiceStartMode.Manual;
        serviceInstaller2.StartType = ServiceStartMode.Manual;

        // ServiceName must equal those on ServiceBase derived classes.
        serviceInstaller1.ServiceName = "Hello-World Service 1";
        serviceInstaller2.ServiceName = "Hello-World Service 2";

        // Add installers to collection. Order is not important.
        Installers.Add(serviceInstaller1);
        Installers.Add(serviceInstaller2);
        Installers.Add(processInstaller);
    }

    public static void Main()
    {
        Console.WriteLine("Usage: InstallUtil.exe [<service>.exe]");
    }
}
Imports System.Collections
Imports System.Configuration.Install
Imports System.ServiceProcess
Imports System.ComponentModel

<RunInstallerAttribute(True)> _
Public Class MyProjectInstaller
    Inherits Installer
    Private serviceInstaller1 As ServiceInstaller
    Private serviceInstaller2 As ServiceInstaller
    Private processInstaller As ServiceProcessInstaller    
    
    Public Sub New()
        ' Instantiate installers for process and services.
        processInstaller = New ServiceProcessInstaller()
        serviceInstaller1 = New ServiceInstaller()
        serviceInstaller2 = New ServiceInstaller()
        
        ' The services will run under the system account.
        processInstaller.Account = ServiceAccount.LocalSystem
        
        ' The services will be started manually.
        serviceInstaller1.StartType = ServiceStartMode.Manual
        serviceInstaller2.StartType = ServiceStartMode.Manual
        
        ' ServiceName must equal those on ServiceBase derived classes.            
        serviceInstaller1.ServiceName = "Hello-World Service 1"
        serviceInstaller2.ServiceName = "Hello-World Service 2"
        
        ' Add installers to collection. Order is not important.
        Installers.Add(serviceInstaller1)
        Installers.Add(serviceInstaller2)
        Installers.Add(processInstaller)
    End Sub

    Public Shared Sub Main()
        Console.WriteLine("Usage: InstallUtil.exe [<service>.exe]")
    End Sub
End Class

備註

ServiceInstaller 針對與其相關聯的服務執行特定的工作。 安裝公用程式會使用它,將與服務相關聯的登錄值寫入HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services登錄機碼內的子機碼。 此服務是由此子機碼內的 ServiceName 所識別。 子機碼也包含服務所屬可執行檔或.dll的名稱。

若要安裝服務,請建立繼承自 Installer 類別的專案安裝程式類別,並將 類別上的 屬性設定 RunInstallerAttributetrue 。 在您的專案中,為每個服務應用程式建立一個 ServiceProcessInstaller 實例,以及應用程式中每個服務的一個 ServiceInstaller 實例。 在專案安裝程式類別建構函式內,使用 ServiceProcessInstallerServiceInstaller 實例設定服務的安裝屬性,並將實例新增至 Installers 集合。

注意

建議您使用 建構函式來新增安裝程式實例;不過,如果您需要在 方法中 Install 新增至 Installers 集合,請務必在 方法中 Uninstall 對集合執行相同的新增。

針對衍生自 Installer 類別的所有類別,集合的狀態 Installers 在 和 Uninstall 方法中 Install 必須相同。 不過,如果您將安裝程式實例新增至 Installers 自訂安裝程式類別建構函式中的集合 Install ,您可以避免跨 和 Uninstall 方法維護集合。呼叫安裝公用程式時,它會尋找 RunInstallerAttribute 屬性。 如果 屬性為 true ,公用程式會安裝已新增至 Installers 與專案安裝程式相關聯之集合的所有服務。 如果 RunInstallerAttributefalse 或 不存在,則安裝公用程式會忽略專案安裝程式。

與專案安裝類別相關聯的 會 ServiceProcessInstaller 安裝專案中所有 ServiceInstaller 實例通用的資訊。 如果此服務具有與安裝專案中其他服務分開的任何專案,則此方法會安裝該服務特定資訊。

注意

請務必與 ServiceName 衍生自 ServiceBase 之類別的 相同 ServiceBase.ServiceName 。 一般而言, ServiceBase.ServiceName 服務的 屬性值是在服務應用程式可執行檔的 Main () 函式內設定。 服務控制管理員會 ServiceInstaller.ServiceName 使用 屬性來尋找此可執行檔內的服務。

您可以在將它新增至 Installers 專案安裝程式的集合之前或之後修改其他屬性 ServiceInstaller 。 例如, StartType 服務可能會設定為在重新開機時自動啟動服務,或要求使用者手動啟動服務。

一般而言,您不會在程式碼中呼叫 方法 ServiceInstaller ;它們通常只能由安裝公用程式呼叫。 安裝公用程式會在安裝程式期間自動呼叫 ServiceProcessInstaller.InstallServiceInstaller.Install 方法。 如有必要,它會在所有先前安裝的元件上呼叫 Rollback (或 ServiceInstaller.Rollback) ,來回複失敗。

安裝公用程式會呼叫 Uninstall 以移除 物件。

應用程式的安裝常式會使用專案安裝程式 Installer.Context 的 自動維護已安裝元件的相關資訊。 此狀態資訊會持續更新為 ServiceProcessInstaller 實例,而且每個 ServiceInstaller 實例都是由 公用程式安裝。 您的程式碼通常不需要明確修改狀態資訊。

執行安裝時,會自動建立 , EventLogInstaller 以安裝與 ServiceBase 衍生類別相關聯的事件記錄檔來源。 Log這個來源的屬性是由 ServiceInstaller 建構函式設定為電腦的 [應用程式記錄檔]。 當您設定 ServiceNameServiceInstaller 應該與 ServiceBase.ServiceName 服務) 相同之 (的 時, Source 會自動將 設定為相同的值。 在安裝失敗中,來源的安裝會與先前安裝的服務一起復原。

如果服務正在執行,此方法 Uninstall 會嘗試停止服務。 不論這是否成功, Uninstall 請復原 所做的 Install 變更。 如果為事件記錄建立新的來源,則會刪除來源。

建構函式

ServiceInstaller()

初始化 ServiceInstaller 類別的新執行個體。

屬性

CanRaiseEvents

取得值,指出元件是否能引發事件。

(繼承來源 Component)
Container

取得包含 IContainerComponent

(繼承來源 Component)
Context

取得或設定有關目前安裝的資訊。

(繼承來源 Installer)
DelayedAutoStart

取得或設定值,這個值表示服務是否應該在其他自動啟動的服務開始執行以前延遲啟動。

Description

取得或設定服務的描述。

DesignMode

取得值,指出 Component 目前是否處於設計模式。

(繼承來源 Component)
DisplayName

表示使用者識別服務的易記名稱。

Events

取得附加在這個 Component 上的事件處理常式清單。

(繼承來源 Component)
HelpText

取得安裝程式集合中所有安裝程式的說明文字。

(繼承來源 Installer)
Installers

取得這個安裝程式包含的安裝程式集合。

(繼承來源 Installer)
Parent

取得或設定安裝程式,含有這個安裝程式所屬的集合。

(繼承來源 Installer)
ServiceName

表示系統用來識別此服務的名稱。 這個屬性必須與您想要安裝之服務的 ServiceName 相同。

ServicesDependedOn

表示要執行這個服務而必須執行的服務。

Site

取得或設定 ComponentISite

(繼承來源 Component)
StartType

表示啟動此服務的方式和時間。

方法

Commit(IDictionary)

當在衍生類別中被覆寫時,完成安裝異動。

(繼承來源 Installer)
CopyFromComponent(IComponent)

ServiceBase 的執行個體複製屬性到這個安裝程式。

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 Component)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。

(繼承來源 Component)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
Install(IDictionary)

藉由將服務應用程式資訊寫入登錄來安裝服務, 這個方法是安裝工具所使用的方法,安裝工具會自動處理適當的方法。

IsEquivalentInstaller(ComponentInstaller)

表示兩種安裝程式是否安裝相同的服務。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
OnAfterInstall(IDictionary)

引發 AfterInstall 事件。

(繼承來源 Installer)
OnAfterRollback(IDictionary)

引發 AfterRollback 事件。

(繼承來源 Installer)
OnAfterUninstall(IDictionary)

引發 AfterUninstall 事件。

(繼承來源 Installer)
OnBeforeInstall(IDictionary)

引發 BeforeInstall 事件。

(繼承來源 Installer)
OnBeforeRollback(IDictionary)

引發 BeforeRollback 事件。

(繼承來源 Installer)
OnBeforeUninstall(IDictionary)

引發 BeforeUninstall 事件。

(繼承來源 Installer)
OnCommitted(IDictionary)

引發 Committed 事件。

(繼承來源 Installer)
OnCommitting(IDictionary)

引發 Committing 事件。

(繼承來源 Installer)
Rollback(IDictionary)

經由安裝程序復原寫入登錄的服務應用程式資訊, 這個方法是安裝工具所使用的方法,安裝工具會自動處理適當的方法。

ToString()

傳回任何包含 Component 名稱的 String。 不應覆寫此方法。

(繼承來源 Component)
Uninstall(IDictionary)

藉由從登錄中移除有關的資訊來解除安裝服務。

事件

AfterInstall

發生於 Installers 屬性中所有安裝程式的 Install(IDictionary) 方法都執行之後。

(繼承來源 Installer)
AfterRollback

發生於 Installers 屬性中所有安裝程式的安裝都復原之後。

(繼承來源 Installer)
AfterUninstall

發生於 Installers 屬性中的所有安裝程式執行其解除安裝作業之後。

(繼承來源 Installer)
BeforeInstall

發生於安裝程式集合中每個安裝程式的 Install(IDictionary) 方法執行之前。

(繼承來源 Installer)
BeforeRollback

發生於 Installers 屬性中的安裝程式復原之前。

(繼承來源 Installer)
BeforeUninstall

發生於 Installers 屬性中的安裝程式執行其解除安裝作業之前。

(繼承來源 Installer)
Committed

發生於 Installers 屬性中的所有安裝程式都認可其安裝之後。

(繼承來源 Installer)
Committing

發生於 Installers 屬性中的安裝程式認可其安裝之前。

(繼承來源 Installer)
Disposed

Dispose() 方法的呼叫處置元件時,就會發生。

(繼承來源 Component)

適用於

另請參閱