WCF Callback function – FileSystemWatcher


FileSystemWatcher class allows to get notification on any change in the directory. The notification can be even filtered by changes like rename, delete and all. File types can be specified to the class to allow notification for a particular file types.

Code Snippet
  1. Dim watchFile As New FileSystemWatcher()
  2.         watchFile.Filter = ConfigurationManager.AppSettings.Item(“FileType”)
  3.         watchFile.IncludeSubdirectories = False
  4.         watchFile.Path = ConfigurationManager.AppSettings.Item(“WatcherPath”)
  5.         AddHandler watchFile.Changed, AddressOf FileNotification
  6.         AddHandler watchFile.Renamed, AddressOf FileNotification
  7.         AddHandler watchFile.Deleted, AddressOf FileNotification
  8.         watchFile.EnableRaisingEvents = True

The file watcher is implemented using the WCF Callback function. Whenever any change in the specified directory an event is triggered and a callback function is called. The WCF client program will listen to the callback function and update the control. For sample program the changes are formed as XML string and returned in the callback. When the callback function is called the xml is updated to the rich text control of the forms client.

Code Snippet
  1. Public Class NotificationImpl
  2.     Implements CallbackClient.ServiceClient.ICallbackServiceCallback
  3.     Private rtx As RichTextBox
  4.     Public Sub New(ByVal MyRtx As RichTextBox)
  5.         rtx = MyRtx
  6.     End Sub
  7.     Public Sub Callback(ByVal OutXml As String) Implements ServiceClient.ICallbackServiceCallback.Callback
  8.         rtx.AppendText(“——-START: “ & DateTime.Now.ToString(“dd/MMM/yyyy hh:mm:ss”) & “———-“ & Chr(13))
  9.         rtx.AppendText(OutXml)
  10.         rtx.AppendText(Chr(13) & “————-END ————–“ & Chr(13))
  11.     End Sub
  12. End Class

The WCF service is implemented using the NetTcp binding. The attached file contains the WCF service library and the Console Host for the service. The second attachment contains the form client to get the notification. The samples are in VB code

Download the Source code from below links…

Callback Service Code

Callback Client

Advertisements

2 thoughts on “WCF Callback function – FileSystemWatcher

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s