Creating Important Aspects of Notification Services
(Page 1 of 7 )
In this final part of a three-part article, you will learn how to create a notification class, event provider, and much more. It is excerpted from chapter 18 of
Programming SQL Server 2005, written by Bill Hamilton (O'Reilly, 2006; ISBN: 0596004796). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
Creating a Notification Class, Content Formatter, and Notification Class Protocol
A notification class named StockWatchNotifications, a content formatter named XsltFormatter, and two notification class protocols named FileandSMTPare created in theCreateNotificationClass()method of Example 18-1. The code follows:
private static void CreateNotificationClass()
{
NotificationClass nc = new NotificationClass(a, "StockWatchNotifications");
NotificationField nf;
nf = new NotificationField(nc, "Symbol");
nf.Type = "nvarchar(6)";
nc.NotificationFields.Add(nf);
nf = new NotificationField(nc, "Price");
nf.Type = "float";
nc.NotificationFields.Add(nf);
ContentFormatter cf = new ContentFormatter(nc, "XsltFormatter");
ContentFormatterArgument cfa;
cfa = new ContentFormatterArgument(cf, "XsltBaseDirectoryPath");
cfa.Value = a.BaseDirectoryPath + @"\AppDefinition";
cf.ContentFormatterArguments.Add(cfa);
cfa = new ContentFormatterArgument(cf, "XsltFileName");
cfa.Value = "StockWatch.xslt";
cf.ContentFormatterArguments.Add(cfa);
nc.ContentFormatter = cf;
nc.DigestDelivery = true;
ProtocolField pf;
// add file notification class protocol
NotificationClassProtocol ncpFile =
new NotificationClassProtocol(nc, "File");
pf = new ProtocolField(ncpFile, "Symbol");
pf.FieldReference = "Symbol";
ncpFile.ProtocolFields.Add(pf);
pf = new ProtocolField(ncpFile, "Price");
pf.FieldReference = "Price";
ncpFile.ProtocolFields.Add(pf);
nc.NotificationClassProtocols.Add(ncpFile);
// add email notification class protocol
NotificationClassProtocol ncpEmail =
new NotificationClassProtocol(nc, "SMTP");
pf = new ProtocolField(ncpEmail, "Subject");
pf.SqlExpression = "'Stock watch: ' + CONVERT(nvarchar(30), GETDATE())";
ncpEmail.ProtocolFields.Add(pf);
pf = new ProtocolField(ncpEmail, "BodyFormat");
pf.SqlExpression = "'html'";
ncpEmail.ProtocolFields.Add(pf);
pf = new ProtocolField(ncpEmail, "From");
pf.SqlExpression = 'notification@StockWatchService.com';
ncpEmail.ProtocolFields.Add(pf);
pf = new ProtocolField(ncpEmail, "Priority");
pf.SqlExpression = "'Normal'";
ncpEmail.ProtocolFields.Add(pf);
pf = new ProtocolField(ncpEmail, "To");
pf.SqlExpression = "DeviceAddress";
ncpEmail.ProtocolFields.Add(pf);
nc.NotificationClassProtocols.Add(ncpEmail);
nc.ExpirationAge = new TimeSpan(1, 0, 0);
a.NotificationClasses.Add(nc);
Console.WriteLine("Added notification class: " + nc.Name);
}
TheNotificationClass object represents a type of notification supported by a Notification Services application. ANotificationFieldobject represents a field in a notification class schema. The notification class in this example has two fields—Symbolof typenvarchar(6)andPriceof typefloat.
The NMO classes for managing notification classes and fields are described in Table 18-7.
Table 18-7. NMO classes for managing notification
classes and fields
| Class | Description |
| NotificationClass | Represents a notification class. |
| NotificationClassCollection | Represents a collection of notification classes as NotificationClassobjects. TheNotificationClasses property of theApplicationclass returns the notification classes for the Notification Services application. |
| NotificationComputedField | Represents a computed field in a notification class schema. |
| NotificationComputedFieldCollection | Represents a collection of computed fields as NotificationComputedFieldobjects. The NotificationComputedFieldsproperty of the NotificationClassclass returns the computed fields for the notification class. |
| NotificationField | Represents a noncomputed field in a notification class schema. |
| NotificationFieldCollection | Represents a collection of fields asNotificationFieldobjects. TheNotificationFieldsproperty of the NotificationClassclass returns the fields for the notification class. |
A content formatter formats notifications for a notification class. Each notification class has one content formatter that can perform different formatting based on field values in the notification. The content formatter takes three arguments:
XsltBaseDirectoryPath
The root directory for all XSLT files.
XsltFileName
The name of the XSLT file used to transform raw
notification data into formatted data for notification
delivery.
DisableEscaping
An optional Boolean argument indicating that the
event data contains either HTML or XML data
preventing further transformation. The default value
isfalse.
The NMO classes for managing content formatters are described in Table 18-8.
Table 18-8. NMO classes for managing content formatters
| Class | Description |
| ContentFormatter | Represents a content formatter. TheContentFormatterproperty of theNotificationClassclass returns the content formatter for the notification class. |
| ContentFormatterArgument | Represents a name-value pair for a content formatter initialization argument. |
| ContentFormatterArgumentCollection | Represents a collection of initialization arguments as ContentFormatterArgumentobjects. The ContentFormatterArgumentsproperty of the ContentFormatterclass returns the initialization arguments for the content formatter. |
A notification class protocol represents a delivery protocol for a notification class. AProtocolFieldobject represents a protocol header field used by some delivery protocols. Protocol field headers are different for file and email notification—examine the code and examine the file and email notifications shown in Figure 18-7 and Figure 18-8 to see the result of setting the protocol fields. The value of theProtocolField object is set using either theSqlExpressionorFieldReferenceproperty. This lets you use either a T-SQL expression or a notification field to define a protocol field value.
The NMO classes for managing protocols are described in Table 18-9.
Table 18-9. NMO classes for managing protocols
Class | Description |
NotificationClassProtocol | Represents a delivery protocol for a notification class. |
NotificationClassProtocolCollection | Represents a collection of notification class protocols as NotificationClassProtocolobjects. The NotificationClassProtocolsproperty of the NotificationClassclass returns the delivery classes for the notification class. |
ProtocolDefinition | Represents a custom delivery protocol. |
ProtocolDefinitionCollection | Represents a collection of custom delivery protocols as ProtocolDefinitionobjects. TheProtocolDefinitions property of theInstanceclass returns the custom delivery protocols for the Notification Services instance. |
ProtocolField | Represents a protocol header field. |
ProtocolFieldCollection | Represents a collection of protocol header fields asProtocolField objects. TheProtocolFieldsproperty of the NotificationClassProtocolclass returns the protocol header fields for the delivery protocol. |
ProtocolRetrySchedule | Represents a retry schedule interval. |
ProtocolRetryScheduleCollection | Represents a collection of retry schedule intervals as ProtocolRetryScheduleobjects. The ProtocolRetrySchedulesproperty of the NotificationClassProtocolclass returns the retry schedules for notifications sent using the delivery protocol. |
Next: Creating an Event Provider >>
More MS SQL Server Articles
More By O'Reilly Media
|
This article is excerpted from chapter 18 of Programming SQL Server 2005, written by Bill Hamilton (O'Reilly, 2006; ISBN: 0596004796). Check it out today at your favorite bookstore. Buy this book now.
|
|