ServiceProcessInstaller 类

定义

安装包含扩展 ServiceBase的类的可执行文件。 安装服务应用程序时,此类由安装实用工具(如 InstallUtil.exe)调用。

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. 假设有一个服务可执行文件,其中包含两个服务“Hello-World 服务 1”和“Hello-World 服务 2”。 在 MyProjectInstaller(由安装实用工具调用)的构造函数中, ServiceInstaller 为每个服务创建对象,并为可执行文件创建一个 ServiceProcessInstaller 对象。 要使安装实用工具将 MyProjectInstaller 识别为有效的安装程序,属性 RunInstallerAttribute 设置为 true

在将安装程序添加到 Installers 集合之前,将在进程安装程序和服务安装程序上设置可选属性。 当安装实用工具访问 MyProjectInstaller 时,将通过调用Installers添加到InstallerCollection.Add集合的对象反过来安装。 在此过程中,安装程序会维护状态信息,指示已安装了哪些对象,因此在安装失败时,可以依次备份每个对象。

通常,不会显式实例化项目安装程序类。 你将创建它并添加 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

注解

ServiceProcessInstaller 操作适用于可执行文件中的所有服务。 安装实用工具使用它来编写与要安装的服务关联的注册表值。

若要安装服务,请创建一个继承自 Installer的项目安装程序类,并将该类设置为 RunInstallerAttributetrue。 在项目中,为每个服务应用程序实例化一ServiceProcessInstallerServiceInstaller实例,并为应用程序中的每个服务实例实例。 最后,将 ServiceProcessInstaller 实例和 ServiceInstaller 实例添加到项目安装程序类。

InstallUtil.exe 运行时,实用工具会在服务程序集中查找设置为 <a0/> 的类。 通过将类添加到与项目安装程序关联的集合,将类添加到 Installers 服务程序集。 RunInstallerAttribute如果是false,安装实用工具将忽略项目安装程序。

对于实例 ServiceProcessInstaller,可以修改的属性包括指定服务应用程序在登录用户以外的帐户下运行。 可以指定服务应运行的特定 UsernamePassword 对,或者可用于 Account 指定服务在计算机的系统帐户、本地或网络服务帐户或用户帐户下运行。

注释

计算机的系统帐户与管理员帐户不同。

通常,你不会在代码中调用方法 ServiceInstaller ;它们通常仅由安装实用工具调用。 安装实用工具会在安装过程中自动调用 ServiceProcessInstaller.InstallServiceInstaller.Install 方法。 如有必要,它会通过调用以前安装的所有组件( RollbackServiceInstaller.Rollback)来回退故障。

应用程序的安装例程使用项目安装程序 Installer.Context自动维护已安装组件的相关信息。 此状态信息会持续更新, ServiceProcessInstaller 因为实例和每个 ServiceInstaller 实例都由实用工具安装。 通常不需要代码显式修改此状态信息。

实例化 ServiceProcessInstaller 导致调用基类构造函数 ComponentInstaller

构造函数

名称 说明
ServiceProcessInstaller()

创建类的新实例 ServiceProcessInstaller

属性

名称 说明
Account

获取或设置运行此服务应用程序的帐户类型。

CanRaiseEvents

获取一个值,该值指示组件是否可以引发事件。

(继承自 Component)
Container

IContainer获取包含 .Component

(继承自 Component)
Context

获取或设置有关当前安装的信息。

(继承自 Installer)
DesignMode

获取一个值,该值指示当前是否 Component 处于设计模式。

(继承自 Component)
Events

获取附加到此 Component对象的事件处理程序的列表。

(继承自 Component)
HelpText

获取为服务安装选项显示的帮助文本。

Installers

获取此安装程序包含的安装程序的集合。

(继承自 Installer)
Parent

获取或设置包含此安装程序所属的集合的安装程序。

(继承自 Installer)
Password

获取或设置与运行服务应用程序的用户帐户关联的密码。

Site

获取或设置 ISiteComponent

(继承自 Component)
Username

获取或设置运行服务应用程序的用户帐户。

方法

名称 说明
Commit(IDictionary)

在派生类中重写时,完成安装事务。

(继承自 Installer)
CopyFromComponent(IComponent)

实现没有CopyFromComponent(IComponent)特定于类的行为的基类ServiceProcessInstaller方法。

CreateObjRef(Type)

创建一个对象,其中包含生成用于与远程对象通信的代理所需的所有相关信息。

(继承自 MarshalByRefObject)
Dispose()

释放该 Component命令使用的所有资源。

(继承自 Component)
Dispose(Boolean)

释放由托管资源使用 Component 的非托管资源,并选择性地释放托管资源。

(继承自 Component)
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetService(Type)

返回一个对象,该对象表示服务由 Component 或其 Container提供的服务。

(继承自 Component)
GetType()

获取当前实例的 Type

(继承自 Object)
InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
Install(IDictionary)

将服务应用程序信息写入注册表。 此方法旨在由安装工具使用,这些工具会自动调用相应的方法。

IsEquivalentInstaller(ComponentInstaller)

确定指定的安装程序是否安装与此安装程序相同的对象。

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

返回包含 String 的名称 Component(如果有)。 不应重写此方法。

(继承自 Component)
Uninstall(IDictionary)

在派生类中重写时,删除安装。

(继承自 Installer)

活动

名称 说明
AfterInstall

Install(IDictionary) 属性中 Installers 所有安装程序的方法运行后发生。

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

适用于

另请参阅