Notification Services—Creating a Notification Services Application and Service 2

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
Database Articles

Notification Services

© 2006 O'Reilly Media, Inc.

CONTINUED: Creating a Notification Services Application and Service

4. Replace the following string constant values in lines 13 to 15:

NSServerName
The server that runs the Notification Services engine components. Use the name of the local computer for this example.
NSUserName
The account the NS$StockWatch service runs under.
NSPassword
The password for the NSUserName account.

5. Compile and execute the code. The results are shown in Figure 18-3.


Image:ProgSQLServer2005fig18-3.jpg
Figure 18-3. Results for Notification Services example


6. Two databases are created when the NMO application is run—StockWatchNSMain and StockWatchStockWatchApp. Ensure that the service login account specified in Step 4 has at least the NSRunService database role membership for both of these databases.

7. Refresh the Notification Services node in Object Explorer in SQL Server Management Studio to view the new Notification Services service, as shown in Figure 18-4.


Image:ProgSQLServer2005fig18-4.jpg
Figure 18-4. Results for StockWatch service example


8. Right-click the StockWatch service and select Start from the context menu to start the service.

9. Right-click the StockWatch service and select Properties from the context menu to display the Instance Properties dialog box, shown in Figure 18-5.


Image:ProgSQLServer2005fig18-5.jpg
Figure 18-5. StockWatch application instance properties


10. Select the Windows Services page to display the service status, as shown in Figure 18-6. The service should be running.

11. Ensure that the SMTP service—a component of Internet Information Services (IIS)—is installed and started. For more information, see Microsoft SQL Server 2005 Books Online.

12. Create the following three folders in the C:\PSS2005\NotificationServices directory:

AppDefinition
The folder containing the XSLT transformation file used to format notifications (StockWatch.xslt in this example) and the schema for the event data (EventSchema.xsd in this example). These two files are discussed in Steps 13 and 14.
Events
The folder in which event data is placed as XML files (named EventData.xml in this example).
Notifications
The folder in which file notifications are created.


Image:ProgSQLServer2005fig18-6.jpg
Figure 18-6. StockWatch Windows Services instance properties


13. Create a file named EventSchema.xsd, as shown in Example 18-2, in the C:\PSS2005\NotificationServices\AppDefinition folder. This file describes the schema of the event data in the EventData.xml file described in Step 15.

Example 18-2. EventSchema.xsd

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:sql="urn:schemas-microsoft-com:mapping-schema">
  <xsd:element name="event" sql:relation="FlightEvents">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Symbol" type="xsd:string" />
        <xsd:element name="Price" type="xsd:float" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

14. Create a file named StockWatch.xslt, as shown in Example 18-3, in the C:\PSS2005\NotificationServices\AppDefinition folder. This file is used to format the notification data for both file and email notifications.

Example 18-3. StockWatch.xslt

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="notifications">
  <html>
    <body>
      StockWatch Price Update<br/><br/>
      <xsl:apply-templates/>
      <br/><br/>
      <i>SQL Server StockWatch Notification Services</i><br/><br/>
    </body>
  </html>
  </xsl:template>
 
  <xsl:template match="notification">
    The price of <b><xsl:value-of select="Symbol" /></b> is now <b>
    $<xsl:value-of select="Price" /></b>.<br/>
    </xsl:template>
</xsl:stylesheet>

15. Create a file named EventData.xml, as shown in Example 18-4, in the C:\PSS2005\NotificationServices folder. This XML file contains the event data. In this example, the events for symbols ABC and DEF have subscriptions and generate notifications. The event for symbol GHI has no subscriptions and does not generate a notification.

Example 18-4. EventData.xml

<eventData>
  <event>
    <Symbol>ABC</Symbol>
    <Price>3.83</Price>
  </event>
  <event>
    <Symbol>DEF</Symbol>
    <Price>5.75</Price>
  </event>
  <event>
    <Symbol>GHI</Symbol>
    <Price>1.22</Price>
  </event>
</eventData>

16. Copy the EventData.xml file into the C:\PSS2005NotificationServices\Events folder to submit events. The File System Watcher event provider reads data from the application, submits the data to the application StockWatchApp, and changes the extension of the event datafile to .done once the file is processed. If there is an error processing the file, the extension of the datafile is changed to .err.

17. After less than a minute, a notification file named FileNotification.txt is created in the C:\PSS2005\NotificationServices\Notifications folder, as shown in Figure 18-7.


Image:ProgSQLServer2005fig18-7.jpg
Figure 18-7. FileNotification.txt


The email message shown in Figure 18-8 is also generated. It appears in the C:\Inetpub\mailroot\Queue folder briefly as a file with an .eml extension and is then moved to the C:\Inetpub\mailroot\Badmail folder because the email address is not valid. To deliver the email, change the email address for the user TonyHamilton in the CreateSubscriber( ) method of Example 18-1 to a valid email address by changing the DeviceAddress property of the SubscriberDevice object for that user.

Notice that both the file and email notifications are formatted using the XSLT transformation StockWatch.xslt, discussed in Step 14.

The remainder of this chapter discusses the code that creates the StockWatch service, subscribers, and subscriptions.


Previous_Page_.gif Next_Page_.gif


Personal tools