Using the Managed Event Submission API

You can use the EventCollector and Event classes in a custom event provider when working with any event source that can provide events as individual items. These classes can be found in the Microsoft.SqlServer.NotificationServices namespace.

EventCollector Object

An EventCollector object gathers events and then submits the event batch to a Notification Services application. When you create an EventCollector object, you initialize it with an application and an event provider name.

Event Object

An Event object encapsulates the data for a single event. When you create an Event object, you initialize the event with an application and an event class name, and then provide values for the event fields. The Event object's fields are defined by the event class schema.

Note

You can create an Event object without initializing it. This allows you to create a "disconnected" Event object that does not have a reference to a Notification Services application. In order to use this technique, you must know the event class name and the event field names at development time.

You create, populate, and submit one Event object for each event record that you want to create. You can reuse an Event object after writing the event to the EventCollector object.

Writing and Committing Events

After you create an Event object and populate it with event data, you add it to an EventCollector object using the Write method.

The EventCollector object also provides an Abort method. Call this method to discard the current event batch.

EventCollector and Event Object Example

This example uses the following namespaces:

  • System
  • Microsoft.SqlServer.NotificationServices
string instanceName = "Tutorial";
string applicationName = "Weather";
string eventClassName = "WeatherEvents";
string eventProviderName = "WeatherSPs";

// Create an NSInstance object.
NSInstance testInstance = new NSInstance(instanceName);

// Create an NSApplication object.
NSApplication testApplication =
    new NSApplication(testInstance, applicationName);

// Create an EventCollector object.
EventCollector testEventCollector =
    new EventCollector(testApplication, eventProviderName);

// Create and define an Event object.
Event evt = new Event(testApplication, eventClassName);
evt["City"] = "Seattle";
evt["Date"] = DateTime.Now;
evt["Low"] = 40;
evt["High"] = 50;
evt["Forecast"] = "Cloudy";

// Write the event to the event collector's batch
testEventCollector.Write(evt);

// Commit the event batch to the application database.
testEventCollector.Commit();

Disconnected Event Object Example

This example shows how to define an event without initializing the Event object with an NSApplication and EventClass.

// Initialize the Event object.
Event testEvent = new Event();
testEvent.EventClassName = "StockEvents";

// Use the Event object.
testEvent["StockSymbol"] = "AWKS";
testEvent["StockPrice"] = "58.35";

See Also

Concepts

Developing Hosted Event Providers
Developing Non-Hosted Event Providers

Other Resources

Developing a Custom Event Provider

Help and Information

Getting SQL Server 2005 Assistance