Share via


主控 Notification Services 引擎

針對每個 Notification Services 執行個體,Notification Services 2.0 會要求您將 Windows 服務安裝在每個執行主控事件提供者、產生器或散發者的伺服器上。對要轉散發通知應用程式的開發人員而言,這會將一或多個 Windows 服務新增至他們的部署,且會建立多個要管理的服務。

在 SQL Server 2005 中,您可以從您自己的應用程式或處理序內主控 Notification Services 引擎。如果您主控引擎,您不安裝 NS$instanceName Windows 服務來執行引擎元件。這會簡化應用程式中的內嵌 Notification Services 解決方案。

主控 Notification Services 引擎

Notification Services 提供一個非常簡單的 API 來主控 Notification Services 引擎。您只需具現化 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);
}

處理錯誤

當執行個體在執行時,它可能會發現執行階段錯誤。這些錯誤會透過 ErrorEventHandler 委託回報給主控處理序,該委託 Error 事件時叫用的。引發事件時,會呼叫該委託來通知主機,並停止執行個體。

下列範例顯示如何回應這個事件:

// 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 服務,然後又同時在應用程式中啟動執行個體。在任一時間點,只有一個處理序的一個執行緒可以擁有執行個體。

如果您啟動 instanceName Windows 服務,然後嘗試在應用程式中啟動執行個體,Notification Services 會發生例外狀況,且您的應用程式無法啟動執行個體。反之亦然:如果您的應用程式正在執行執行個體,您又嘗試啟動 Windows 服務,Windows 服務便無法啟動。

如果在該伺服器上擁有執行個體引擎的執行緒失敗了,該處理序內的其他執行緒便可取得執行個體的擁有權。

ms171236.note(zh-tw,SQL.90).gif附註:
如果您主控引擎,用來啟動及停止執行個體的標準工具 (如 SQL Server Management Studio、net 命令及控制台中的 [服務]) 便無法用來啟動及停止執行個體。

安全性需求

應用程式或處理序負責控制主控事件提供者、產生器及散發者的作業,且必須有適當的 Windows 與資料庫權限來執行相關工作。這包括 SQLServer2005NotificationServicesUser$ComputerName Windows 群組中的成員資格,以及具有一或多個適當 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 協助