When a device provider should raise the DeviceConnectionDownEvent is a topic of some confusion. The Contoso device provider sample gave a wrong example of its use till recently. The sample has the following code inside the PhysicalDeviceProxy.Close method:

 

DeviceConnectionDownEvent mgmtEvent = new DeviceConnectionDownEvent("Closing connection for device " + this.DeviceInformation.DeviceId);

Notification notification = new Notification((DeviceManagementEvent)mgmtEvent);

this.raiseNotificationEvent(new NotificationEventArgs(notification));

 

While there is nothing wrong with this code in itself, the place where it is being executed is wrong. The PhysicalDeviceProxy(PDP)'s Close method is called by the RFID platform when the user closes a device connection. Also, the consumer of the raised DeviceConnectionDownEvent event is also the RFID platform. So this is what happens: a user closes a device connection -> the RFID platform calls PDP.Close() -> PDP.Close() raises the DeviceConnectionDownEvent -> the RFID platform receives this event and thinks that the provider has detected that a device cannot be cannot be contacted any more and takes some action accordingly.

The RFID platform expects the DeviceConnectionDownEvent to inform it of a device connection going down. Raising the event when the platform itself has closed the device connection defeats the purpose. So the above code, while valid, should be taken out of the PDP.Close method. It should be called by the provider if it proactively detects that a device cannot be contacted.