Daily Archives: August 15, 2010

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

WCF Service Error Hosting – Adding reference to netTcp binding service error

 

System.InvalidOperationException: Could not find a base address that matches scheme net.tcp for the endpoint with binding NetTcpBinding. Registered base address schemes are [http].
   at System.ServiceModel.ServiceHostBase.MakeAbsoluteUri(Uri relativeOrAbsoluteUri, Binding binding, UriSchemeKeyedCollection baseAddresses)

image

Metadata contains a reference that cannot be resolved: ‘net.tcp://localhost:8732/Callback.ServiceLibrary/CallbackService/’.
The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was ’00:05:00′.
An existing connection was forcibly closed by the remote host
If the service is defined in the current solution, try building the solution and adding the service reference again.

Resolution:

step 1: Adding the mex metadata to the config solved the issue

Add the below endpoint to the service node

<endpoint address="mex"
                 binding="mexTcpBinding"
                 contract="IMetadataExchange"/>

step 2: Add the Service metadata tag to the service behaviors node

<serviceBehaviors>
        <behavior name="Callback.ServiceLibrary.CallbackServiceBehavior">
          <serviceDebug includeExceptionDetailInFaults="False" />
          <serviceMetadata policyVersion="Policy15"/>
        </behavior>
      </serviceBehaviors>

step 3: Use the url net.Tcp://localhost:8732/mex to add the service reference not the full url