Notification Services エンジンのホスト

Notification Services 2.0 では、Notification Services のインスタンスごとに、ホストされるイベント プロバイダ、ジェネレータ、またはディストリビュータを実行する各サーバーに Windows サービスをインストールする必要がありました。このため、通知アプリケーションを再配布する開発者は、1 つ以上の Windows サービスを配置に追加し、管理用にもう 1 つのサービスを作成していました。

SQL Server 2005 では、独自のアプリケーションまたはプロセス内から Notification Services エンジンをホストできます。エンジンをホストする場合は、エンジン コンポーネントを実行するために NS$instanceName Windows サービスをインストールする必要はありません。これにより、アプリケーションへの Notification Services ソリューションの埋め込みが簡略化されます。

Notification Services エンジンのホスト

Notification Services には、Notification Services エンジンをホストするための非常に単純な API が用意されています。単に NSInstance クラスをインスタンス化して、ホストするインスタンスの名前を指定するだけです。その後に StartInstance メソッドを呼び出してエンジンを開始します。エンジンを停止するには、StopInstance メソッドを呼び出します。以下に、エンジンの停止と開始に必要なコードの例を示します。

try
{
    // Instantiate the Notification Services instance.
    NSInstance nsInstance = new NSInstance("FlightInstance");

    // Start the instance.
    Console.WriteLine("Starting instance...");
    nsInstance.StartInstance();

    // Check the IsRunning property to verify that 
    // this process is running the instance.
    if (nsInstance.IsRunning == true)
        Console.WriteLine("The instance is running.");
    else
        Console.WriteLine("The instance is NOT running!");

    // Stop instance.
    Console.WriteLine("Stopping instance...");
    nsInstance.StopInstance();

}
catch (Exception ex)
{
    // Write exception message to the console.
    Console.WriteLine(ex.Message);
}

エラー処理

インスタンスの実行中に、ランタイム エラーが発生する場合があります。これらのエラーは、Error イベントがトリガされると呼び出される ErrorEventHandler デリゲートを介して、ホストしているプロセスに報告されます。イベントが発生すると、このデリゲートが呼び出されてホストへの通知を行い、インスタンスは停止されます。

次の例は、このイベントへの応答を示しています。

// Define an error handler for the hosted execution engine.
private void InstanceErrorHandler(object sender, ErrorEventArgs e)
{
    Console.WriteLine (e.GetException ().ToString ());
}

// Start and stop the hosted execution engine.
public void ErrorHandlingStartStop()
{
    try
    {
        // Instantiate the Notification Services instance.
        NSInstance nsInstance = new NSInstance("FlightInstance");

        // Add an error handler to the instance.
        nsInstance.Error += 
            new NSInstance.ErrorEventHandler(InstanceErrorHandler);

        // Start the instance.
        Console.WriteLine("Starting instance...");
        nsInstance.StartInstance();

        // Check the IsRunning property to verify that 
        // this process is running the instance.
        if (nsInstance.IsRunning == true)
            Console.WriteLine("The instance is running.");
        else
            Console.WriteLine("The instance is NOT running!");


        // Stop the instance.
        Console.WriteLine("Stopping instance...");
        nsInstance.StopInstance();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

送信側は、NSInstance オブジェクトです。

Notification Services のインスタンスの実行

ホストされるイベント プロバイダ、ジェネレータ、およびディストリビュータをローカル コンピュータで実行するエンジンを実行するには、次の操作を行う必要があります。

  • ホストされるイベント プロバイダ、ジェネレータ、またはディストリビュータを実行する各サーバーに Notification Services エンジン コンポーネントをインストールします。
  • これらの各サーバーにインスタンスを登録します。インスタンスを登録するときに、Windows サービスを作成する必要はありません。ホストされるエンジンは、Windows サービスに代わる役割を果たすことができます。
  • これらの各サーバーにアプリケーションをインストールします。
  • インスタンス化を行ってこれらの各サーバーでインスタンスを開始するコードを呼び出します。

Windows サービスをインストールしている場合でも、Windows サービスを開始した後で、同時にアプリケーションでインスタンスを開始することはできません。インスタンスを "所有" できるのは、一度に 1 つのプロセスの 1 つのスレッドだけです。

NS$instanceName Windows サービスを開始してから、アプリケーションでインスタンスを開始しようとすると、Notification Services は例外をスローして、アプリケーションはインスタンスを開始しません。逆の場合も同様です。アプリケーションがインスタンスを実行している場合に、Windows サービスを開始しようとしても、Windows サービスを開始することはできません。

そのサーバー上のインスタンスのエンジンを所有するスレッドが失敗すると、そのプロセス内の他のスレッドがインスタンスの所有権を引き継ぐことができます。

ms171236.note(ja-jp,SQL.90).gifメモ :
エンジンをホストしている場合は、SQL Server Management Studio、net コマンド、[コントロール パネル] の [サービス] など、インスタンスの開始と停止に使用する標準ツールを使用してインスタンスを開始および停止することはできません。

セキュリティ要件

アプリケーションまたはプロセスが、ホストされるイベント プロバイダ、ジェネレータ、およびディストリビュータの操作を制御している場合、関連の作業を行うためには適切な Windows 権限とデータベース権限が必要です。これには、Windows グループ SQLServer2005NotificationServicesUser$ComputerName のメンバシップと、Notification Services の適切なデータベース ロールのメンバシップを含みます。Notification Services のインスタンスの実行に必要な権限の詳細については、「Notification Services のインスタンスの Windows アカウントの構成」および「Notification Services のインスタンスの SQL Server 権限の構成」を参照してください。

Notification Services コンポーネントの埋め込み

アプリケーションに Notification Services を埋め込んでいる場合は、次のアセンブリを含めるようにします。

  • Microsoft.SqlServer.NotificationServices.dll
  • Microsoft.SqlServer.smo.dll
  • nscontrol.exe

SQL Server 2005 コンポーネントの再配布については、SQL Server 2005 の使用許諾契約書を参照してください。

参照

概念

Notification Services のセキュリティに関する注意点
Notification Services のインスタンスの Windows アカウントの構成
Notification Services のインスタンスの SQL Server 権限の構成

その他の技術情報

Notification Services の配置

ヘルプおよび情報

SQL Server 2005 の参考資料の入手