宿主 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);
}

处理错误

当实例运行时,可能会遇到运行时错误。这些错误会通过在触发 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 服务的同时不能在应用程序中启动实例。在任一时刻只有一个进程的一个线程可以“拥有”实例。

如果启动了 NS$instanceName Windows 服务,然后尝试在应用程序中启动实例,则 Notification Services 将引发异常,您的应用程序将不会启动实例。反之亦然:应用程序正在运行实例时,此时如果尝试启动 Windows 服务,则 Windows 服务将无法启动。

如果在服务器上拥有实例引擎的线程失败,则该进程中的其他线程可能接管该实例。

ms171236.note(zh-cn,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 帮助