DeviceConnection revisited...
One client connection vs. multiple client connections:
We saw an overview of the DeviceConnection class (Microsoft.Rfid.Client) in an earlier post and how it can be used for configuration and operations on a device. Now, consider the scenario where there is a client application which has opened a connection to a particular device and is doing some configuration on it (say for e.g. SetProperty). If another client application wants to open a connection to the same device to configure/operate it, DeviceConnection.Open() doesn't allow it because having more than one application configuring/operating on the device at the same time could affect the state of the device and could potentially take the device to an undefined state. Hence, when the second client application tries to do a deviceConnection.Open(), it would get an exception such as:
"Cannot open a connection to device <devicename> because another client has already connected to the device. Retry the operation later"
That said, there is an override if your application is really sure of what it is doing and has the required administrative privileges: DeviceConnection.OpenAdministrationConnection(). You can use this if you are sure that you are not stepping on some other client that is connected to the device.
Device Security:
What privileges are needed for using the DeviceConnection methods? Any operation that can potentially affect/change the state of the device requires the caller to have administrative privileges on the device. Having administrative privilege on a device means that atleast one of the following should be true:
1) The caller is a member of the builtin administrators group (OR)
2) The caller is a member of the device's custom administrator list (To view/modify this using RFID Manager, right click on a device -> Security)
So, using DeviceConnection.OpenAdministrationConnection() or DeviceConnection.SetProperty() or DeviceConnection.ApplyPropertyProfile() are considered "state changing operations" and hence would require one of the above two privileges. Using DeviceConnection.Open() or DeviceConnection.GetProperty() or DeviceConnection.PrintTag() are not considered to change state and hence don't require the above privileges - just being a member of the RFID_USER group (a Windows group created by BizTalk RFID during its installation) is enough to do such operations. Note that tag related operations (GetTags(), PrintTag() etc.) fall into the latter category. By default, devices in BizTalk RFID inherit security options from the parent device group.
Using DeviceConnection from an event handler:
If you have an event handler running as part of a RFID process, you may want to use DeviceConnection to do operations such as getting/setting the I/O port value on a device. This scenario is considered to be very similar to any other standalone client application using DeviceConnection. Let's say you want to do a state changing device operation from an event handler such as DeviceConnection.SetProperty(). If you see an exception such as the one below, it means the context in which the event handler is running doesn't have the required privileges to do the operation:
System.IO.SensorServices.Rfid.Client.DeviceManagerException: Insufficient permissions to perform the requested operation on device entity <device name>.
at System.IO.SensorServices.Rfid.Client.DeviceConnection.ExecuteCommand[TCommand](String deviceSourceName, TCommand cmd, VendorDefinedParameters parameters)
at System.IO.SensorServices.Rfid.Client.DeviceConnection.SetProperty(String sourceName, String groupName, String propertyName, Object value, VendorDefinedParameters parameters)
An event handler runs in the context of the "RFID Service Account (RSA)" on Windows XP / Windows Vista; it runs in the context of a "Worker Process Account (PSA") on a Windows Server 2003 computer. If this account is not part of the device's custom administrator list, it means that it does not have the privileges to do the operation. You can resolve this easily by adding PSA (on a Windows Server 2003 computer) or the RSA (on a Windows XP or Windows Vista computer) to the device's administrator list, using RFID Manager -> Right click on the device or device group -> Security -> Add.
To summarize, any client application or an event handler using the DeviceConnection.Open() or DeviceConnection.OpenAdministrationConnection() is considered to be a "synchronous client". There could be any number of running "RFID Processes" bound to a device and be receiving notifications from the device. Internally, BizTalk RFID maintains only a single connection to the device and closes this connection only when the final interested process or the client goes away. I will post more on the device connection management in a later post.