Export (0) Print
Expand All

Subscribing to Events

This topic explains how to subscribe to events. DPWS supports event subscription under the WS-Eventing specification, allowing other devices to register with your device to receive notifications when something happens. Subscriptions are handled automatically by the .NET Micro Framework DPWS device stack, so the stack needs to know which events each service can provide. Therefore the service class declares the events it can provide as part of its constructor, as follows:
      // Add event source
      DpwsWseEventSource sampleEvent = new DpwsWseEventSource("smpl",
      "http://schemas.example.org/SampleService", "SampleEvent");
      EventSources.Add(sampleEvent);
    

To send an event, use the Dpws.Device.Services.DpwsWseSubscriptionMgr.FireEvent method. This method requires a reference to the DpwsHostedService, a reference to the DpwsWseEventSource, and a byte array containg the SOAP message to be sent. The latter is typically constructed by a method in the service, using System.Ext.Xml.XmlWriter.

The following is an example of raising an event SampleEvent in the service SampleService, assuming references to the service and the event source were stored in the variables sampleService and sampleEvent when they were instantiated, as shown in the last code sample:

      // build the message and fire the event
      Dpws.Device.Services.DpwsWseSubscriptionMgr.FireEvent(sampleService, sampleEvent,
      sampleService.BuildSampleEventMessage());
    
Show:
© 2015 Microsoft