An NMO Sample Application


This chapter introduces a new sample application that illustrates the capabilities of the NMO API to define, create, register, enable, and delete a new SQL-NS instance. Think of this application as a single program that performs all the operations you normally do with separate SQL-NS commands to create an instance and prepare it to run.

The SQL-NS instance defined by the sample application is essentially the same StockBroker instance we looked at in Chapter 3, "The Simplest Notification Application: Stock Quotes." The same instance and application definitions are used, but here they are specified through NMO instead of XML. We also use the same steps we used in Chapter 3 to get the instance running. But instead of using the SQL-NS tools in Management Studio (or their command-line equivalents), we invoke these steps programmatically with the NMO API.

Although the SQL-NS instance we create in this chapter is the same in content as the one created in Chapter 3, we distinguish it with a unique name. Chapter 3's instance is called StockBroker; the instance we create in this chapter is called StockBrokerNMO.

The code for the StockBrokerNMO sample is packaged into a Visual Studio solution, located at C:\SQL-NS\Samples\StockBrokerNMO. The solution file is named StockBrokerNMO.sln. Open this in Visual Studio now and refer to it as you read through this chapter. This section explains the organization of the code in the solution and illustrates the basic structure of the application, before we explore the details of the NMO API.

The StockBrokerNMO solution has three main source files:

  • Program.csContains the main driver code for the application

  • Arguments.csContains classes used to parse the application's command-line arguments

  • StockBrokerNMO.csContains the StockBrokerNMO class that encapsulates the core functionality of the application

The outline of the StockBrokerNMO class is shown in Listing 16.1. Notice that the three namespaces associated with NMO and SMO are declared in using statements at the start of the code.

Listing 16.1. Structure of the StockBrokerNMO Class

 ... using Microsoft.SqlServer.Management.Nmo; using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Common; namespace StockBrokerNMO {    public class StockBrokerNMO    {       ...       public StockBrokerNMO(           string sqlServer,           AuthenticationMode authenticationMode,           string sqlUserName,           string sqlPassword,           string baseDirectoryPath,           string serviceUserName,           string servicePassword,           AuthenticationMode serviceAuthenticationMode,           string serviceSqlUserName,           string serviceSqlPassword)       {           ...       }       public void Cleanup()       {           ...       }       public void Create()       {           ...       }       public void Register()       {           ...       }       public void Enable()       {           ...       }    } } 

The class consists of a public constructor and four public methods that implement the various operations required to create and administer the SQL-NS instance. We examine the implementations of the constructor and these methods (which use the NMO API) in the sections "Using NMO to Define and Create an Instance" (p. 537) and "Using NMO to Administer an Instance" (p. 549) later in this chapter.

The code in Program.cs drives the application by invoking the methods of the StockBrokerNMO class, as shown in Listing 16.2.

Listing 16.2. Calling the Methods of the StockBrokerNMO Class in the Program's Main() Driver Method

 ... namespace StockBrokerNMO {     ...     class Program     {         ...         static int Main(string[] args))         {            ...            try            {                // Instantiate the StockBrokerNMO class.                StockBrokerNMO stockBrokerNMO = new StockBrokerNMO(                    arguments["-sqlServer"].ModifierValue,                    authenticationMode,                    sqlUserName,                    sqlPassword,                    arguments["-baseDirectoryPath"].ModifierValue,                    arguments["-serviceUserName"].ModifierValue,                    arguments["-servicePassword"].ModifierValue,                    serviceAuthenticationMode,                    serviceSqlUserName,                    serviceSqlPassword);                // Do cleanup first, to get rid of any previous copy                // of the instance.                Console.WriteLine("Cleaning up Instance");                stockBrokerNMO.Cleanup();                // Create the instance.                Console.WriteLine("Creating StockBrokerNMO Instance");                stockBrokerNMO.Create();              // Register the instance.              Console.WriteLine("Registering Instance");              stockBrokerNMO.Register();              // Enable the instance.              Console.WriteLine("Enabling Instance");              stockBrokerNMO.Enable();          }          catch (Exception ex)          {              ...          }          ...       }    } } 

This code creates an instance of the StockBrokerNMO class, obtaining the constructor's parameter values from various command-line arguments. It then calls the Cleanup() method on the StockBrokerNMO class to clean up any existing copy of the StockBrokerNMO instance that might have been created by a previous run of the program. This clears the way to create a new instance, by calling the Create() method. Calls to the Register() and Enable() methods register and enable the instance, respectively.

For the purposes of this chapter, you can ignore the other code in Program.cs, not shown in Listing 16.2. Most of this code deals with processing and validating the application's command-line arguments.

The code in the StockBrokerNMO solution is compiled into a single executable, StockBrokerNMO.exe. Instructions for invoking StockBrokerNMO.exe and supplying the command-line arguments it requires are provided in the section "Testing the StockBrokerNMO Sample Application" (p. 554) after we've examined the usage of the NMO API in the rest of the code.




Microsoft SQL Server 2005 Notification Services
Microsoft SQL Server 2005 Notification Services
ISBN: 0672327791
EAN: 2147483647
Year: 2006
Pages: 166
Authors: Shyam Pather

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net