Creating a Notification Services Instance and Application
The Notification Services instance named StockWatch and a Notification Services application named StockWatchApp are created in theMain()method of Example 18-1. The code follows—the code that creates the Notification Services instance and application is highlighted:
static void Main(string[] args) { Server server = new Server("(local)");
Figure 18-8. StockWatch notification email
// create a new instance NotificationServices ns = server.NotificationServices; nsi = new Instance(ns, "StockWatch");
CreateDeliveryChannel();
// create a new application in the StockWatch instance a = new Application(nsi, "StockWatchApp"); a.BaseDirectoryPath = baseDirectoryPath;
Console.WriteLine(Environment.NewLine + "Press any key to continue."); Console.ReadKey(); }
TheNotificationServicesobject represents a Notification Services server. TheInstancesproperty of theNotificationServices class returns anInstanceCollectionobject containing a collection of Notification Services instances asInstance objects. At a minimum, you must define aDeliveryChannel object and anApplicationobject to create anInstanceobject—in this example, this is done by theCreateDeliveryChannel()andMain()methods.
TheRegisterLocal()method of theInstance class registers an instance of Notification Services on the local computer. This is the same as registering a Notification Services instance using SQL Server Management Studio by right-clicking the Notification Services instance and selecting Tasks -> Register from the context menu. You can also register a Notification Services instance by using thenscontrol registercommand. Registering an instance creates or updates registry entries for the instance, creates performance counters, and optionally creates a Windows Service to run the instance.
TheEnable()method of theInstanceclass enables all instance and application components, allowing event collection, notification generation, notification distribution, and subscription management. ANotificationServicesinstance is disabled when you create it.
The NMO classes used to manage Notification Services instances are described in Table 18-1.
Table 18-1. NMO classes for managing Notification Services instances
Class
Description
Instance
Represents a Notification Services instance.
InstanceCollection
Represents a collection of instances asInstanceobjects. TheInstancesproperty of the NotificationServicesclass returns the Notification Services instances on the server.
TheApplicationobject represents a Notification Services application. At a minimum, you must define aGeneratorobject and aDistributorobject for anApplicationobject—in this example, this is done by theCreateGenerator()andCreateDistributor()methods.
This example configures theApplicationobject by setting the following properties:
BaseDirectoryPath Specifies the base directory path for the ADF
QuantumDuration Specifies how frequently the generator tries to process work
PerformanceQueryInterval Specifies how frequently the application updates its performance counters
SubscriptionQuantumLimit Specifies how far the logical clock can fall behind the real-time clock before skipping subscription rule firings
ChronicleQuantumLimit Specifies how far the logical clock can fall behind the real-time clock before skipping event chronicle firings
VacuumRetentionAge Specifies the minimum age at which event and notification data is considered obsolete and can be removed
The NMO classes used to manage Notification Services applications are described in Table 18-2.
Table 18-2. NMO classes for managing applications
Class
Description
Application
Represents a Notification Services application.
ApplicationCollection
Represents a collection of Notification Services applications asApplicationobjects. The Applicationsproperty of theInstanceclass returns the Notification Services applications hosted on the Notification Services instance.
A file delivery channel named StockWatchFileDeliveryChannel and an email delivery channel named StockWatchEmailDeliveryChannel are created in the CreateDeliveryChannel()method of Example 18-1. The code follows:
You have to add at least one delivery channel to a Notification Services instance before creating it. TheProtocolNameproperty of theDeliveryChannel object must be set toSMTP,File, or the name of a custom delivery protocol.
The NMO classes used to manage delivery channels are described in Table 18-3.
Table 18-3. NMO classes for managing delivery channels
Delivery channel
Description
DeliveryChannel
Represents a delivery channel.
DeliveryChannelArgument
Represents a name-value pair specifying delivery channel configuration and authentication information for the delivery service.
DeliveryChannelArgumentCollection
Represents a collection of delivery channel arguments asDelivery-ChannelArgumentobjects. TheDeliveryChannelArguments property of theDeliveryChannelclass returns the delivery channel arguments for the delivery channel.
DeliveryChannelCollection
Represents a collection of delivery channels asDeliveryChannel objects. TheDeliveryChannelsproperty of theInstanceclass returns the delivery channels for the Notification Services instance.
EventField ef; ef = new EventField(ec, "Symbol"); ef.Type = "nvarchar(6)"; ec.EventFields.Add(ef); ef = new EventField(ec, "Price"); ef.Type = "float"; ec.EventFields.Add(ef);
An event class represents a type of event used by a Notification Services application. The event class has two fields—Symbolof typenvarchar(6)andPriceof typefloat.
The NMO classes for managing event classes, fields, and chronicles are described inTable 18-4.
Table 18-4. NMO classes for managing events
Class
Description
EventChronicle
Represents an event chronicle.
EventChronicleCollection
Represents a collection of event chronicles asEventChronicleobjects. TheEventChroniclesproperty of theEventClassclass returns the event chronicles for the event class.
EventChronicleRule
Represents an event chronicle maintenance query that the generator runs.
EventClass
Represents an event class.
EventClassCollection
Represents a collection of event classes asEventClassobjects. TheEvent-Classesproperty of theApplicationclass returns the event classes for the Notification Services application.
EventField
Represents a field in an event class schema.
EventFieldCollection
Represents a collection of event class schema fields asEventFieldobjects. The EventFieldsproperty of theEventClassclass returns the event fields for the event class.
A subscription class with a single subscription event rule is created in the CreateSubscriptionClassMethod() method of Example 18-1. The code follows:
CreateSubscriptionClassMethod( ) method of Example 18-1. The code follows:A subscription class with a single subscription event rule is created in the CreateSubscriptionClassMethod( ) method of Example 18-1. The code follows:
SubscriptionField sf; sf = new SubscriptionField(sc, "DeviceName"); sf.Type = "nvarchar(255)"; sc.SubscriptionFields.Add(sf); sf = new SubscriptionField(sc, "SubscriberLocale"); sf.Type = "nvarchar(10)"; sc.SubscriptionFields.Add(sf); sf = new SubscriptionField(sc, "Symbol"); sf.Type = "nvarchar(6)"; sc.SubscriptionFields.Add(sf); sf = new SubscriptionField(sc, "Price"); sf.Type = "float"; sc.SubscriptionFields.Add(sf);
SubscriptionEventRule ser = new SubscriptionEventRule(sc, "StockWatchSubscriptionsEventRule"); ser.Action = @"INSERT INTO StockWatchNotifications (" + "SubscriberId, DeviceName, SubscriberLocale, Symbol, Price) " + "SELECT s.SubscriberId, s.DeviceName, s.SubscriberLocale, " + "e.Symbol, e.Price " + "FROM StockWatchEvents e, StockWatchSubscriptions s " + "WHERE e.Symbol = s.Symbol";
ASubscriptionClass object defines a type of subscription within a Notification Services application. TheSubscriptionField objects added to theSubscriptionobject represent fields in the subscription class schema. ASubscriptionChronicleobject lets you store subscription information outside of tables used by the subscription class—this example does not use a subscription chronicle.
The NMO classes for managing subscription chronicles, subscription classes, and subscription fields are described in Table 18-5.
Table 18-5. NMO classes for managing subscription chronicles, classes, and fields
Class
Description
SubscriptionChronicle
Represents a subscription chronicle.
SubscriptionChronicleCollection
Represents a collection of subscription chronicles as SubscriptionChronicleobjects. TheSubscriptionChronicles property of theSubscriptionClassclass returns the subscription chronicles for the subscription class.
SubscriptionClass
Represents a subscription class.
SubscriptionClassCollection
Represents a collection of subscription classes asSubscriptionClass objects. TheSubscriptionClassesproperty of theApplication class returns the subscription classes for the Notification Services application.
SubscriptionField
Represents a field in the subscription class schema.
SubscriptionFieldCollection
Represents a collection of fields asSubscriptionFieldobjects. The SubscriptionFieldsproperty of theSubscriptionClassclass returns the fields for the subscription class schema.
ASubscriptionEventRule object represents a rule that uses T-SQL queries to generate notifications when event batches arrive. TheActionproperty represents the T-SQL query for theSubscriptionEventRuleobject. In this example, notifications are generated when the ticker symbol of an event matches the ticker symbol specified in a subscription.
The NMO classes for managing the different types of subscription rules are described in Table 18-6.
Table 18-6. NMO classes for managing subscription rules
Class
Description
SubscriptionCondition EventRule
Represents a subscription rule that the generator runs against subscriptions that use conditions to generate notifications.
SubscriptionCondition EventRuleCollection
Represents a collection of subscription condition event rules as SubscriptionConditionEventRule objects. The subscriptionConditionEvent-Rulesproperty of the SubscriptionClassclass returns the subscription condition event rules for the subscription class.
SubscriptionCondition ScheduledRule
Represents a subscription rule that the generator runs against scheduled subscriptions that use conditions to generate notifications.
SubscriptionCondition ScheduledRuleCollection
Represents a collection of subscription condition scheduled rules as SubscriptionConditionScheduled-Ruleobjects. The SubscriptionCondition-ScheduledRulesproperty of the SubscriptionClassclass returns the subscription condition scheduled rules for the subscription class.
SubscriptionEventRule
Represents an event rule that contains simple (not conditional) actions.
SubscriptionEvent RuleCollection
Represents a collection of subscription event rules as SubscriptionEventRuleobjects. The SubscriptionEventRulesproperty of the SubscriptionClassclass returns the subscription event rules for the subscription class.
SubscriptionScheduled Rule
Represents a scheduled rule that contains actions that do not use conditions to generate notifications.
SubscriptionScheduled RuleCollection
Represents a collection of scheduled rules as SubscriptionScheduledRuleobjects. The SubscriptionScheduledRulesproperty of the SubscriptionClassclass returns the scheduled rules for the subscription class.
Please check back next week for the conclusion of this article.