In this chapter, we continue working with the music store application that we've been developing over the past several chapters. We extend it with several new event providers that illustrate the various aspects of event provider development in SQL-NS. This section describes the layout of the source files containing this chapter's code and the steps you need to take to get ready to use the working examples. Source FilesAs in previous chapters, we're going to modify the source code for the music store application in the C:\SQL-NS\Samples\MusicStore directory. Supplemental source files containing the code we look at in this chapter are provided in the C:\SQL-NS\Chapters\08\SupplementaryFiles directory. Table 8.1 provides a summary of the contents of these supplementary files.
As before, the actual ICF and ADF that the compiler reads are C:\SQL-NS\Samples\MusicStore\InstanceConfiguration.xml and C:\SQL-NS\Samples\MusicStore\SongAlerts\ApplicationDefinition.xml. In this chapter, when you're instructed to add code, you can either edit these files manually, or you can copy over one of the supplemental files. You then update the instance as you did in previous chapters to compile the new code. The source files for the custom event providers that we build in this chapter are in the C:\SQL-NS\Samples\MusicStore\SongAlerts\CustomComponents directory. We examine these files in the sections "Building Custom Hosted Event Providers" (p. 264) and "Building Standalone Event Providers" (p. 291). Changes to the Scripts for Argument EncryptionAs in previous chapters, we'll continue using batch files in the Scripts subdirectory under C:\SQL-NS\Samples\MusicStore to create, manipulate, and configure the SQL-NS instance. Many of these are the same as the ones you've seen in previous chapters, but some new ones are introduced to accommodate argument encryption, which we use in this chapter. As explained in the "Argument Encryption" section (p. 237), you enable argument encryption by setting the value of the <EncryptArguments> element in the ICF to true. Listing 8.1 showed the fragment of the ICF we'll use in this chapter that sets this element. When we invoke the nscontrol create, register, and update commands, we need to pass the argument key value. This is done by means of the -argumentkey parameter, which each of these commands takes. In the Scripts directory, you'll find scripts called create_with_argument_key.cmd, register_with_argument_key.cmd, and update_with_argument_key.cmd. These are equivalent to the scripts you used in previous chapters (create.cmd, register.cmd, and update.cmd, respectively) except that the nscontrol commands in the new scripts are invoked with the -argumentkey parameter. As an example, let's look at the call to nscontrol create in create_with_argument_key.cmd:
The boldface portion of the command supplies the string, MusicStoreArgumentKey, as the value of the -argumentkey parameter. You can use any string value for the argument key; I've chosen the rather unimaginative value you see here purely for simplicity. Take a look at the other two scripts to see how the -argumentkey parameter is used with nscontrol register and nscontrol update. Note Whatever argument key you decide to use, it's important that you pass the same value for the -argumentkey parameter to nscontrol create, register, and update. The steps in the following section instruct you to use create_with_argument_key.cmd to create the music store instance from scratch and register_with_argument_key.cmd to register it. When you update the instance to add the new code for this chapter, make sure you use update_with_argument_key.cmd instead of update.cmd. If you use the wrong script, the argument key won't be supplied to nscontrol update, resulting in a compile error.
Starting from a Clean InstanceAlthough this chapter extends the prototype of the music store application that you built in previous chapters, you will need to recompile the music store instance from scratch, rather than just update the version you created earlier. Besides ensuring that you're starting from a clean instance (free of any artifacts that may have developed from earlier testing and debugging), this is necessary because we are now using argument encryption. Recall that it's not possible to update an existing instance to turn on argument encryption: You must first delete and then re-create the instance. Before starting the work of this chapter, perform the following instructions to establish a clean instance:
Caution If you accidentally invoked register.cmd instead of register_with_argument_key.cmd in step 7, the argument key will not be written to the Registry. The call to nscontrol register in register.cmd does not specify an argument key value (the -argumentkey parameter is omitted entirely). As a result, the SQL-NS service will fail to start. Because the instance uses argument encryption, the service cannot do any processing if it can't retrieve the argument key from the Registry. If you use the net start command to start the service, you'll see an error message that reads, A system error has occurred. Also, an event message will be written to the Application Event Log. To see this message, open the Event Viewer from Administrative Tools, select the Application log, and look for a message with NotificationServices as the event source name and an event ID of 6. The description in the message will read: The argument key was not provided but an argument key was given when the database was created. To correct the situation, first unregister the instance using the unregister.cmd script (also located in the C:\SQL-NS\Samples\MusicStore\Scripts directory). Then run register_with_argument_key.cmd. You do not need to add subscribers and subscriptions. Because we're focusing on event submission, all the tests in this chapter go only as far as making sure that the correct events end up in the events table. The tests do not check how those events are matched with subscriptions. We can do this with confidence because we've already tested the matching capabilities of the application in previous chapters. We know that when events end up in the events table, the matching logic in the application works. This chapter just presents several alternative ways to get events into the events table. It is a testament to the elegant design of the SQL-NS architecture that this kind of modular testing is possible. The AddSongs ProgramIn the music store application, events represent songs added to the music store's catalog. In previous chapters, we simulated events by inserting the IDs of songs already in the music store catalog into the event class view. We never actually added any new songs to the catalog. Some event providers in this chapter do look at the data in the music store catalog to determine whether new songs have been added. To test these event providers, we need a way to add new song records to the catalog tables. I've provided a program called AddSongs that presents a simple user interface for doing this. A Visual Studio solution containing the source code for this program is in the C:\SQL-NS\Samples\MusicStore\SongAlerts\AddSongs directory. Before continuing, build this solution so that you can run the program when it's needed later:
At this point, it's not necessary for you to go through the source code for the AddSongs program. The "Submitting Events from an Application" section (p. 296) later in this chapter describes how this program can act as a standalone event provider; in that section, we look at the parts of its source code involved in event submission. For now, just start the program in Visual Studio (by going to the Debug menu and selecting Start Debugging, or by pressing F5) so that you can learn how to use it. Caution For AddSongs to work correctly, the music store database must already have been created. Before running the AddSongs program, make sure that you've completed the instructions in the "Starting from a Clean Instance" section (p. 241). When AddSongs starts, it opens a database connection dialog box in which you specify how to connect to the music store database. Enter the name of the SQL Server hosting the database and select either Windows Authentication or SQL Server Authentication. If you select SQL Server Authentication, supply the username and password of the development account you created in Chapter 2, "Getting Set Up." Click OK to connect to the database. After AddSongs connects to the database, you should see a window like the one shown in Figure 8.4. Figure 8.4. The AddSongs program.AddSongs allows you to enter a new album, with up to five songs. It's obviously a rather limited application. It does not allow you to add songs to existing albums, browse the songs already in the database, or enter more than five songs. However, for the simple purpose of adding songs to the database to test event providers, it is sufficient. Enter an album title and artist name into the first two text boxes and pick a genre from the drop-down list. You can then enter up to five songs and click Add to Database to create the album and song records in the music store catalog. For now, leave the Submit Events for Songs Added box unchecked. I'll explain the use of this check box later in this chapter. Take the time to play with AddSongs and familiarize yourself with it. Later in this chapter, you'll be asked to use it to add songs to the database. Note After having built the AddSongs program as instructed in this section, you can start it later by invoking the AddSongs.exe executable directly from the solution's binaries directory, C:\SQL-NS\Samples\MusicStore\SongAlerts\AddSongs\bin\Debug. Installing Message QueuingOne of the event provider examples in this chapter uses Message Queuing, an optional Windows component. If you don't have this component installed, you should install it now. If you choose not to install Message Queuing, you will still be able to work through most of the material in this chapter. You just won't be able to run the continuous hosted event provider described later. To install Message Queuing, use the following instructions:
|