ServiceInstaller 类

定义

安装一个类,该类扩展 ServiceBase 来实现服务。Installs a class that extends ServiceBase to implement a service. 在安装服务应用程序时由安装实用工具调用该类。This class is called by the install utility when installing a service application.

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 该程序从继承 InstallerThe following example creates a project installer, called MyProjectInstaller, which inherits from Installer. 假设有一个服务可执行文件,其中包含两个服务: "Hello World Service 1" 和 "Hello World Service 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 of these services, and a ServiceProcessInstaller is created for the executable. 为了使安装实用工具识别 MyProjectInstaller 为有效的安装程序,该 RunInstallerAttribute 特性设置为 trueFor the install utility to recognize MyProjectInstaller as a valid installer, the RunInstallerAttribute attribute is set to true.

在将安装程序添加到集合之前,在进程安装程序和服务安装程序上设置可选属性 InstallersOptional 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 can be backed out in turn, if an installation failure occurs.

通常,不会显式创建项目安装程序类的实例。Normally, you would not create an instance of your project installer class explicitly. 您将创建它并将 RunInstallerAttribute 属性添加到语法中,但它是实际调用的安装实用工具,因而实例化类。You would create it and add the RunInstallerAttribute attribute to the syntax, but it is the install utility that 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

注解

ServiceInstaller确实适用于与其关联的服务。The ServiceInstaller does work specific to the service with which it is associated. 安装实用工具使用它将与服务关联的注册表值写入 HKEY_LOCAL_MACHINE \System\CurrentControlSet\Services 注册表项中的子项。It is used by the installation utility to write registry values associated with the service to a subkey within the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services registry key. 服务由其在此子项中的 ServiceName 标识。The service is identified by its ServiceName within this subkey. 子项还包括服务所属的可执行文件或 .dll 的名称。The subkey also includes the name of the executable or .dll to which the service belongs.

若要安装服务,请创建一个从类继承的项目安装程序类 Installer ,并将 RunInstallerAttribute 类的特性设置为 trueTo install a service, create a project installer class that inherits from the Installer class, and set the RunInstallerAttribute attribute on the class to true. 在项目中, ServiceProcessInstaller 为每个服务应用程序创建一个实例,并 ServiceInstaller 为应用程序中的每个服务创建一个实例。Within your project, create one ServiceProcessInstaller instance per service application, and one ServiceInstaller instance for each service in the application. 在项目安装程序类构造函数中,使用和实例设置服务的安装 ServiceProcessInstaller 属性 ServiceInstaller ,并将实例添加到集合中 InstallersWithin your project installer class constructor, set the installation properties for the service using the ServiceProcessInstaller and ServiceInstaller instances, and add the instances to the Installers collection.

备注

建议使用构造函数来添加安装程序实例;但是,如果需要 Installers 在方法中将添加到集合中 Install ,请确保在方法中对集合执行相同的添加 UninstallIt is recommended that you use the constructor for adding installer instances; however, if you need to add to the Installers collection in the Install method, be sure to perform the same additions to the collection in the Uninstall method.

对于从类派生的所有类 Installer ,集合的状态 Installers 在和方法中必须相同 Install UninstallFor all classes deriving from the Installer class, the state of the Installers collection must be the same in the Install and Uninstall methods. 但是, Install Uninstall 如果将安装程序实例添加到 Installers 自定义安装程序类构造函数中的集合,则可以避免在和方法中维护集合。在调用安装实用程序时,它会查找 RunInstallerAttribute 属性。However, you can avoid the maintenance of the collection across the Install and Uninstall methods if you add installer instances to the Installers collection in your custom installer class constructor.When the install utility is called, it looks for the RunInstallerAttribute attribute. 如果该属性为 true ,则实用工具将安装已添加到 Installers 集合中并与项目安装程序关联的所有服务。If the attribute is true, the utility installs all the services that were added to the Installers collection that were associated with your project installer. 如果 RunInstallerAttributefalse 或不存在,则安装实用工具会忽略项目安装程序。If RunInstallerAttribute is false or does not exist, the install utility ignores the project installer.

ServiceProcessInstaller与项目安装类关联的将安装对项目中的所有实例都通用的信息 ServiceInstallerThe ServiceProcessInstaller associated with your project installation class installs information common to all ServiceInstaller instances in the project. 如果此服务具有与安装项目中的其他服务分隔的任何内容,则此方法会安装该服务特定的信息。If this service has anything that separates it from the other services in the installation project, that service-specific information is installed by this method.

备注

与从派生的类的相同,这一点非常重要 ServiceName ServiceBase.ServiceName ServiceBaseIt is crucial that the ServiceName be identical to the ServiceBase.ServiceName of the class you derived from ServiceBase. 通常情况下,服务的 ServiceBase.ServiceName 属性的值在服务应用程序的可执行文件的主 ( # A1 函数内设置。Normally, the value of the ServiceBase.ServiceName property for the service is set within the Main() function of the service application's executable. 服务控制管理器使用 ServiceInstaller.ServiceName 属性在此可执行文件中查找服务。The Service Control Manager uses the ServiceInstaller.ServiceName property to locate the service within this executable.

你可以在 ServiceInstaller 将其添加到 Installers 项目安装程序的集合之前或之后修改的其他属性。You can modify other properties on the ServiceInstaller either before or after adding it to the Installers collection of your project installer. 例如, StartType 可以将服务设置为在重新启动时自动启动服务,或者要求用户手动启动该服务。For example, a service's StartType may be set to start the service automatically at reboot or require a user to start the service manually.

通常情况下,你不会在 ServiceInstaller 你的代码内调用方法; 通常仅由安装实用工具调用这些方法。Normally, you will 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.

安装实用工具调用 Uninstall 以删除对象。The installation utility calls Uninstall to remove the object.

应用程序的安装例程使用项目安装程序的,自动维护有关已安装组件的信息 Installer.ContextAn 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 state information explicitly.

执行安装时,它会自动创建 EventLogInstaller 以安装与派生类关联的事件日志源 ServiceBaseWhen the installation is performed, it automatically creates an EventLogInstaller to install the event log source associated with the ServiceBase derived class. Log此源的属性由 ServiceInstaller 构造函数设置到计算机的应用程序日志。The Log property for this source is set by the ServiceInstaller constructor to the computer's Application log. 当您设置 ServiceName (的以使其与 ServiceInstaller 服务) 的相同时,会 ServiceBase.ServiceName Source 自动将设置为相同的值。When you set the ServiceName of the ServiceInstaller (which should be identical to the ServiceBase.ServiceName of the service), the Source is automatically set to the same value. 在安装失败时,源的安装将与以前安装的服务一起回滚。In an installation failure, the source's installation is rolled-back along with previously installed services.

如果服务正在运行,则该 Uninstall 方法将尝试停止它。The Uninstall method tries to stop the service if it is running. 无论是否成功,都 Uninstall 将撤消所做的更改 InstallWhether this succeeds or not, Uninstall undoes the changes made by Install. 如果为事件日志记录创建了新源,则会删除该源。If a new source was created for event logging, the source is deleted.

构造函数

ServiceInstaller()

初始化 ServiceInstaller 类的新实例。Initializes a new instance of the ServiceInstaller class.

属性

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)
DelayedAutoStart

获取或设置一个值,该值指示是否应延迟启动该服务,直到运行其他自动启动的服务。Gets or sets a value that indicates whether the service should be delayed from starting until other automatically started services are running.

Description

获取或设置服务的说明。Gets or sets the description for the service.

DesignMode

获取一个值,用以指示 Component 当前是否处于设计模式。Gets a value that indicates whether the Component is currently in design mode.

(继承自 Component)
DisplayName

指示向用户标识服务的友好名称。Indicates the friendly name that identifies the service to the user.

Events

获取附加到此 Component 的事件处理程序的列表。Gets the list of event handlers that are attached to this Component.

(继承自 Component)
HelpText

获取安装程序集合中所有安装程序的帮助文字。Gets the help text for all the installers in the installer collection.

(继承自 Installer)
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)
ServiceName

指示系统用于标识此服务的名称。Indicates the name used by the system to identify this service. 此属性必须与要安装的服务的 ServiceName 相同。This property must be identical to the ServiceName of the service you want to install.

ServicesDependedOn

指示为使该服务能够运行而必须正在运行的服务。Indicates the services that must be running for this service to run.

Site

获取或设置 ComponentISiteGets or sets the ISite of the Component.

(继承自 Component)
StartType

指示启动此服务的方式和时间。Indicates how and when this service is started.

方法

Commit(IDictionary)

在派生类中重写时,完成安装事务。When overridden in a derived class, completes the install transaction.

(继承自 Installer)
CopyFromComponent(IComponent)

将来自 ServiceBase 的一个实例的属性复制到此安装程序。Copies properties from an instance of ServiceBase to this installer.

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。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 占用的非托管资源,还可以另外再释放托管资源。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()
已过时。

检索控制此实例的生存期策略的当前生存期服务对象。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)

通过向注册表写入服务应用程序信息来安装服务。Installs the service by writing service application information to the registry. 该方法旨在由自动处理适当方法的安装工具使用。This method is meant to be used by installation tools, which process the appropriate methods automatically.

IsEquivalentInstaller(ComponentInstaller)

指示两个安装程序是否会安装相同的服务。Indicates whether two installers would install the same service.

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. 该方法旨在由自动处理适当方法的安装工具使用。This method is meant to be used by installation tools, which process the appropriate methods automatically.

ToString()

返回包含 Component 的名称的 String(如果有)。Returns a String containing the name of the Component, if any. 不应重写此方法。This method should not be overridden.

(继承自 Component)
Uninstall(IDictionary)

通过从注册表中移除关于服务的信息来卸载服务。Uninstalls the service by removing information about it from the registry.

事件

AfterInstall

Installers 属性中的所有安装程序的 Install(IDictionary) 方法都运行后发生。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)

适用于

另请参阅