次の方法で共有


ServiceInstaller クラス

ServiceBase を拡張するクラスをインストールしてサービスを実装します。このクラスは、サービス アプリケーションのインストール時に、インストール ユーティリティで呼び出されます。

この型のすべてのメンバの一覧については、ServiceInstaller メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Configuration.Install.Installer
            System.Configuration.Install.ComponentInstaller
               System.ServiceProcess.ServiceInstaller

Public Class ServiceInstaller
   Inherits ComponentInstaller
[C#]
public class ServiceInstaller : ComponentInstaller
[C++]
public __gc class ServiceInstaller : public ComponentInstaller
[JScript]
public class ServiceInstaller extends ComponentInstaller

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

ServiceInstaller は、関連付けられているサービスに固有の処理を行います。このクラスは、インストール ユーティリティが、サービスに関連付けられているレジストリ値を HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services レジストリ キー内のサブキーに書き込む場合に使用されます。サービスは、このサブキー内の ServiceName によって識別されます。サブキーには、サービスが属する実行可能ファイルまたは DLL (ダイナミック リンク ライブラリ) の名前も含まれます。

サービスをインストールするには、 Installer クラスから継承するプロジェクト インストーラ クラスを作成し、そのクラスの RunInstallerAttribute 属性を true に設定します。プロジェクト内では、サービス アプリケーションごとに 1 つの ServiceProcessInstaller インスタンス、およびアプリケーション内の各サービスにつき 1 つの ServiceInstaller インスタンスをインスタンス化します。最後に、 ServiceProcessInstaller インスタンスおよび ServiceInstaller インスタンスをプロジェクト インストーラ クラスに追加します。

インストール ユーティリティは、呼び出されると RunInstallerAttribute 属性を検索します。属性が true の場合、ユーティリティは、プロジェクト インストーラに関連付けられた Installers コレクションに追加されたすべてのサービスをインストールします。 RunInstallerAttributefalse の場合、または存在しない場合、インストール ユーティリティはプロジェクト インストーラを無視します。

メモ    ServiceName は、 ServiceBase から派生したクラスの ServiceBase.ServiceName と必ず同じにする必要があります。通常、サービスの ServiceBase.ServiceName プロパティの値は、サービス アプリケーションの実行可能ファイルの Main() 関数内で設定されます。サービス コントロール マネージャは、 ServiceInstaller.ServiceName プロパティを使用して、この実行可能ファイル内でサービスを検索します。

ServiceInstaller のその他のプロパティは、プロジェクト インストーラの Installers コレクションに追加する前または後に変更できます。たとえば、サービスの StartType で、サービスが再起動時に自動的に起動するのか、ユーザーが手動で起動するようにするのかを設定できます。

通常、コード内の ServiceInstaller では、これらのメソッドを呼び出しません。これらのメソッドを呼び出すのは、一般にはインストール ユーティリティだけです。インストール ユーティリティは、インストール プロセス中に、 ServiceProcessInstaller.Install メソッドと ServiceInstaller.Install メソッドを自動的に呼び出します。必要に応じて、インストール済みのすべてのコンポーネントで Rollback (または ServiceInstaller.Rollback) を呼び出すことによって、エラーを回復します。

インストール ユーティリティで Uninstall を呼び出してオブジェクトを削除します。

アプリケーションのインストール ルーチンは、既にインストールされているコンポーネントに関する情報を、プロジェクト インストーラの Installer.Context を使用して自動的に維持します。この状態情報は、 ServiceProcessInstaller インスタンスとして継続的に更新されます。各 ServiceInstaller インスタンスは、ユーティリティによってインストールされます。通常、コードでは状態情報を明示的に変更する必要はありません。

インストールが実行されると、 EventLogInstaller が自動的に作成されて、 ServiceBase 派生クラスに関連付けられているイベント ログ ソースがインストールされます。このソースの Log プロパティは、 ServiceInstaller コンストラクタによって、コンピュータのアプリケーション ログに設定されます。 ServiceInstallerServiceName (サービスの ServiceBase.ServiceName と同一にする必要があります) を設定すると、 Source は自動的に同じ値に設定されます。インストールに失敗すると、ソースのインストールは、前にインストールしたサービスの内容に合わせてロールバックされます。

サービスが実行中の場合、 Uninstall メソッドは停止しようとします。停止が成功したかどうかにかかわらず、 UninstallInstall で行われた変更を元に戻します。イベント ログに対して新しいソースが作成されると、このソースは削除されます。

使用例

[Visual Basic, C#, C++] Installer から継承される、 MyProjectInstaller という名前のプロジェクト インストーラを作成する例を次に示します。この例では、2 つのサービス "Hello-World Service 1" と "Hello-World Service 2" を格納する、サービスの実行可能ファイルがあることを前提にしています。インストール ユーティリティによって呼び出される MyProjectInstaller のコンストラクタ内では、これらの各サービスに対し ServiceInstaller オブジェクトが作成され、実行可能ファイルに対して ServiceProcessInstaller が作成されます。インストール ユーティリティで MyProjectInstaller を有効なインストーラとして認識できるように、 RunInstallerAttribute 属性は true に設定されます。

[Visual Basic, C#, C++] インストーラが Installers コレクションに追加される前に、オプションのプロパティがプロセス インストーラおよびサービス インストーラに設定されます。インストール ユーティリティが MyProjectInstaller にアクセスすると、 InstallerCollection.Add の呼び出しを通じて Installers コレクションに追加されたオブジェクトが順番にインストールされます。このプロセス中に、インストーラは、インストールされているオブジェクトを示す状態情報を保持します。このため、インストールに失敗した場合でも、それぞれのオブジェクトを順番に回復できます。

[Visual Basic, C#, C++] 通常、プロジェクト インストーラ クラスは明示的にインスタンス化しません。プロジェクト インストーラ クラスを作成して、 RunInstallerAttribute 属性を構文に追加しますが、実際にクラスを呼び出してインスタンス化するのはインストール ユーティリティで行います。

 
Imports System
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
End Class

' </Sn

[C#] 
using System;
using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;
using System.ComponentModel;

[RunInstallerAttribute(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);
   }
}


[C++] 
#using <mscorlib.dll>
#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;

[RunInstallerAttribute(true)]
public __gc class MyProjectInstaller: public Installer{
private:
   ServiceInstaller* serviceInstaller1;
   ServiceInstaller* serviceInstaller2;
   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 = S"Hello-World Service 1";
      serviceInstaller2->ServiceName = S"Hello-World Service 2";

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

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.ServiceProcess

プラットフォーム: Windows NT Server 4.0, Windows NT Workstation 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Serviceprocess (System.Serviceprocess.dll 内)

参照

ServiceInstaller メンバ | System.ServiceProcess 名前空間 | ServiceBase.ServiceName | ServiceProcessInstaller | ServiceBase | EventLog