Michael Wolf
{Binding ME}
Monday January 18, 2010
Silverlight 4 breaking the com sandbox to create a file watcher
Since Silverlight 4's release I have become quire enamored with the elevated privileges out of browser functionality. The com interopability is great, but again your back to com development and interop. Which although very powerful is also kind of painful. Recently some one brought up a question of how you would accomplish something like a file watcher. In a desktop / server experience this would be pretty simple using something like a FileSystemWatcher (http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx ) in the full clr. However no such luck with in the Silverlight 4 agclr. Yet with the ability to execute processes from the out of browser applications in Silverlight 4 on windows, I decided to create a bridge service. A bridge service is more or less a full trust soap or tcpip service which runs on the desktop which the Silverlight application can communicate with. This approach is more or less what is used in the flex world via the merapi project (http://www.merapiproject.net/ ). After the bridge is in place, you can add a full .net FileSystemWatcher to the service, and publish back to the
Silverlight application when a file has been added/removed.
Policy/Socket Service
The creation of the bridge service is pretty straight forward. You will need to implement a duplex style pub/sub service, a good example can be found here http://tomasz.janczuk.org/2009/11/pubsub-sample-with-wcf-nettcp-protocol.html . This service could be a http pooling duplex, but in my example I used the new net.tcp binding due to the performance improvement. In addition you will need to set up and have running a policy server. The net.tcp binding enforces the same need for a policy server, and the port restriction as the Silverlight 3 socket support. Luckily now with in visual studio there is a decent policy service project template you can add from the online template gallery. In the example shown, this was the basis for the net.tcp service its self. Below is the code for setting up the net.tcp service in code with out config.
For more info on the new net.tcp binding in silverlight 4 see:
- http://www.silverlightshow.net/items/WCF-NET.TCP-Protocol-in-Silverlight-4.aspx
- http://tomasz.janczuk.org/2009/11/wcf-nettcp-protocol-in-silverlight-4.html
Silverlight to Bridge Communication
After the bridge is in place you communicate with the service like any other duplex style service, by adding connect methods and responding to the service call back delegates. In visual studio you even get full discoverability via the add service reference dialog. The last step is simply to add the com interop to actually start the service exe.
In addition on start up you could pull down the exe if its not currently downloaded (see http://www.cynergysystems.com/blogs/page/michaelwolf?entry=silverlight_4_and_command_line for further examples of this).





