ServiceProcessInstaller 類別

定義

Installs an executable containing classes that extend ServiceBase.Installs an executable containing classes that extend ServiceBase. This class is called by installation utilities, such as InstallUtil.exe, when installing a service application.This class is called by installation utilities, such as InstallUtil.exe, when installing a service application.

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

範例

下列範例會建立名為 MyProjectInstaller 的專案安裝程式, 它Installer會繼承自。The following example creates a project installer called MyProjectInstaller, which inherits from Installer. 假設有一個服務可執行檔, 其中包含兩個服務: 「Hello-World 服務1」和「Hello-World 服務2」。It is assumed there is a service executable that contains two services, "Hello-World Service 1" and "Hello-World Service 2". 在 MyProjectInstaller 的函式中 (會由安裝公用程式呼叫), ServiceInstaller會針對每個服務建立物件, ServiceProcessInstaller並為可執行檔建立。Within the constructor for MyProjectInstaller (which would be called by the install utility), ServiceInstaller objects are created for each service, and a ServiceProcessInstaller is created for the executable. 若要讓安裝公用程式將 MyProjectInstaller 辨識為有效的安裝RunInstallerAttribute程式, 屬性會true設定為。For the install utility to recognize MyProjectInstaller as a valid installer, the RunInstallerAttribute attribute is set to true.

在將安裝程式新增至Installers集合之前, 會在進程安裝程式和服務安裝程式上設定選擇性屬性。Optional properties are set on the process installer and the service installers before the installers are added to the Installers collection. 當安裝公用程式存取 MyProjectInstaller 時, 透過Installers InstallerCollection.Add呼叫加入集合中的物件將會輪流安裝。When the install utility accesses MyProjectInstaller, the objects added to the Installers collection through a call to InstallerCollection.Add will be installed in turn. 在此過程中, 安裝程式會維護狀態資訊, 指出已安裝的物件, 因此, 每個物件都可以在安裝失敗時輪流備份。During the process, the installer maintains state information indicating which objects have been installed, so each object can be backed out in turn in case of an installation failure.

一般來說, 您不會明確地具現化您的專案安裝程式類別。Normally, you would not instantiate your project installer class explicitly. 您會建立它並加入RunInstallerAttribute, 但是安裝公用程式實際上會呼叫, 因此會具現化類別。You would create it and add the RunInstallerAttribute, but the install utility actually calls, and therefore instantiates, the class.

#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

備註

適用ServiceProcessInstaller于可執行檔中的所有服務。The ServiceProcessInstaller does work common to all services in an executable. 安裝公用程式會使用它來寫入與您要安裝之服務相關聯的登錄值。It is used by the installation utility to write registry values associated with services you want to install.

若要安裝服務, 請建立繼承自Installer的專案安裝程式類別, 並將類別上的RunInstallerAttribute設定true為。To install a service, create a project installer class that inherits from Installer, and set the RunInstallerAttribute on the class to true. 在您的專案中, ServiceProcessInstaller為每個服務應用程式具ServiceInstaller現化一個實例, 並針對應用程式中的每個服務, 各一個實例Within your project, instantiate one ServiceProcessInstaller instance per service application, and one ServiceInstaller instance for each service in the application. 最後, 將ServiceProcessInstaller實例ServiceInstaller和實例新增至您的專案安裝程式類別。Finally, add the ServiceProcessInstaller instance and the ServiceInstaller instances to your project installer class.

當 installutil.exe 執行時, 公用程式會尋找服務元件中的RunInstallerAttribute類別, 並將設為。 trueWhen InstallUtil.exe runs, the utility looks for classes in the service assembly with the RunInstallerAttribute set to true. 藉由將類別加入至與您的專案安裝Installers程式相關聯的集合, 將其加入至服務元件。Add classes to the service assembly by adding them to the Installers collection associated with your project installer. 如果RunInstallerAttributefalse, 安裝公用程式會忽略專案安裝程式。If RunInstallerAttribute is false, the install utility ignores the project installer.

對於的實例ServiceProcessInstaller, 您可以修改的屬性包括指定服務應用程式在登入的使用者以外的帳戶下執行。For an instance of ServiceProcessInstaller, properties you can modify include specifying that a service application run under an account other than the logged-on user. 您可以指定服務應該Username執行Password的特定和配對, 也可以使用Account來指定以電腦的系統帳戶、本機或網路服務帳戶或使用者帳戶來執行服務。You can specify a particular Username and Password pair under which the service should run, or you can use Account to specify that the service run under the computer's System account, a local or network service account, or a user account.

注意

電腦的系統帳戶與管理員帳戶不同。The computer's System account is not the same as the Administrator account.

一般來說, 您不會在ServiceInstaller程式碼中呼叫方法; 通常只有安裝公用程式才會呼叫它們。Normally, you do not call the methods on ServiceInstaller within your code; they are generally called only by the install utility. 安裝公用程式會在安裝ServiceProcessInstaller.Install過程ServiceInstaller.Install中自動呼叫和方法。The install utility automatically calls the ServiceProcessInstaller.Install and ServiceInstaller.Install methods during the installation process. 如有必要, 它會在所有先前安裝Rollback的元件ServiceInstaller.Rollback上呼叫 (或), 以進行失敗。It backs out failures, if necessary, by calling Rollback (or ServiceInstaller.Rollback) on all previously installed components.

應用程式的安裝常式會使用專案安裝程式Installer.Context, 自動維護已安裝元件的相關資訊。An application's install routine maintains information automatically about the components already installed, using the project installer's Installer.Context. 此狀態資訊會隨著ServiceProcessInstaller實例而持續更新, 而且每個ServiceInstaller實例都是由公用程式所安裝。This state information is continuously updated as the ServiceProcessInstaller instance and each ServiceInstaller instance is installed by the utility. 您的程式碼通常不需要明確地修改此狀態資訊。It is usually unnecessary for your code to modify this state information explicitly.

具現化ComponentInstaller ServiceProcessInstaller會導致呼叫基類的函式。Instantiating a ServiceProcessInstaller causes the base class constructor, ComponentInstaller, to be called.

建構函式

ServiceProcessInstaller()

Creates a new instance of the ServiceProcessInstaller class.Creates a new instance of the ServiceProcessInstaller class.

屬性

Account

Gets or sets the type of account under which to run this service application.Gets or sets the type of account under which to run this service application.

CanRaiseEvents

取得值,指出元件是否能引發事件。Gets a value indicating whether the component can raise an event.

(繼承來源 Component)
Container

取得包含 IContainerComponentGets the IContainer that contains the Component.

(繼承來源 Component)
Context

取得或設定有關目前安裝的資訊。Gets or sets information about the current installation.

(繼承來源 Installer)
DesignMode

取得值,指出 Component 目前是否處於設計模式。Gets a value that indicates whether the Component is currently in design mode.

(繼承來源 Component)
Events

取得附加在這個 Component 上的事件處理常式清單。Gets the list of event handlers that are attached to this Component.

(繼承來源 Component)
HelpText

Gets help text displayed for service installation options.Gets help text displayed for service installation options.

Installers

取得這個安裝程式包含的安裝程式集合。Gets the collection of installers that this installer contains.

(繼承來源 Installer)
Parent

取得或設定安裝程式,含有這個安裝程式所屬的集合。Gets or sets the installer containing the collection that this installer belongs to.

(繼承來源 Installer)
Password

Gets or sets the password associated with the user account under which the service application runs.Gets or sets the password associated with the user account under which the service application runs.

Site

取得或設定 ComponentISiteGets or sets the ISite of the Component.

(繼承來源 Component)
Username

Gets or sets the user account under which the service application will run.Gets or sets the user account under which the service application will run.

方法

Commit(IDictionary)

當在衍生類別中被覆寫時,完成安裝異動。When overridden in a derived class, completes the install transaction.

(繼承來源 Installer)
CopyFromComponent(IComponent)

Implements the base class CopyFromComponent(IComponent) method with no ServiceProcessInstaller class-specific behavior.Implements the base class CopyFromComponent(IComponent) method with no ServiceProcessInstaller class-specific behavior.

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(繼承來源 MarshalByRefObject)
Dispose()

釋放 Component 所使用的所有資源。Releases all resources used by the Component.

(繼承來源 Component)
Dispose(Boolean)

釋放 Component 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。Releases the unmanaged resources used by the Component and optionally releases the managed resources.

(繼承來源 Component)
Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetLifetimeService()

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
GetService(Type)

傳回表示 Component 或其 Container 所提供之服務的物件。Returns an object that represents a service provided by the Component or by its Container.

(繼承來源 Component)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
InitializeLifetimeService()

取得存留期服務物件,以控制這個執行個體的存留期原則。Obtains a lifetime service object to control the lifetime policy for this instance.

(繼承來源 MarshalByRefObject)
Install(IDictionary)

Writes service application information to the registry.Writes service application information to the registry. This method is meant to be used by installation tools, which call the appropriate methods automatically.This method is meant to be used by installation tools, which call the appropriate methods automatically.

IsEquivalentInstaller(ComponentInstaller)

判斷指定的安裝程式和這個安裝程式是否安裝相同的物件。Determines if the specified installer installs the same object as this installer.

(繼承來源 ComponentInstaller)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。Creates a shallow copy of the current MarshalByRefObject object.

(繼承來源 MarshalByRefObject)
OnAfterInstall(IDictionary)

引發 AfterInstall 事件。Raises the AfterInstall event.

(繼承來源 Installer)
OnAfterRollback(IDictionary)

引發 AfterRollback 事件。Raises the AfterRollback event.

(繼承來源 Installer)
OnAfterUninstall(IDictionary)

引發 AfterUninstall 事件。Raises the AfterUninstall event.

(繼承來源 Installer)
OnBeforeInstall(IDictionary)

引發 BeforeInstall 事件。Raises the BeforeInstall event.

(繼承來源 Installer)
OnBeforeRollback(IDictionary)

引發 BeforeRollback 事件。Raises the BeforeRollback event.

(繼承來源 Installer)
OnBeforeUninstall(IDictionary)

引發 BeforeUninstall 事件。Raises the BeforeUninstall event.

(繼承來源 Installer)
OnCommitted(IDictionary)

引發 Committed 事件。Raises the Committed event.

(繼承來源 Installer)
OnCommitting(IDictionary)

引發 Committing 事件。Raises the Committing event.

(繼承來源 Installer)
Rollback(IDictionary)

Rolls back service application information written to the registry by the installation procedure.Rolls back service application information written to the registry by the installation procedure. This method is meant to be used by installation tools, which process the appropriate methods automatically.This method is meant to be used by installation tools, which process the appropriate methods automatically.

ToString()

傳回任何包含 Component 名稱的 StringReturns a String containing the name of the Component, if any. 不應覆寫此方法。This method should not be overridden.

(繼承來源 Component)
Uninstall(IDictionary)

當在衍生類別中被覆寫時,移除安裝。When overridden in a derived class, removes an installation.

(繼承來源 Installer)

事件

AfterInstall

發生於 Install(IDictionary) 屬性中的所有安裝程式的 Installers 方法都已經執行之後。Occurs after the Install(IDictionary) methods of all the installers in the Installers property have run.

(繼承來源 Installer)
AfterRollback

發生於 Installers 屬性中所有安裝程式的安裝都復原之後。Occurs after the installations of all the installers in the Installers property are rolled back.

(繼承來源 Installer)
AfterUninstall

發生於 Installers 屬性中的所有安裝程式執行其解除安裝作業之後。Occurs after all the installers in the Installers property perform their uninstallation operations.

(繼承來源 Installer)
BeforeInstall

發生於安裝程式集合中的每個安裝程式的 Install(IDictionary) 方法執行之前。Occurs before the Install(IDictionary) method of each installer in the installer collection has run.

(繼承來源 Installer)
BeforeRollback

發生於 Installers 屬性中的安裝程式復原之前。Occurs before the installers in the Installers property are rolled back.

(繼承來源 Installer)
BeforeUninstall

發生於 Installers 屬性中的安裝程式執行其解除安裝作業之前。Occurs before the installers in the Installers property perform their uninstall operations.

(繼承來源 Installer)
Committed

發生於 Installers 屬性中的所有安裝程式都認可其安裝之後。Occurs after all the installers in the Installers property have committed their installations.

(繼承來源 Installer)
Committing

發生於 Installers 屬性中的安裝程式認可其安裝之前。Occurs before the installers in the Installers property commit their installations.

(繼承來源 Installer)
Disposed

當此元件由 Dispose() 方法的呼叫處置時發生。Occurs when the component is disposed by a call to the Dispose() method.

(繼承來源 Component)

適用於

另請參閱