// Stop watch the current file, if any. public void StopWatching() { try { LocalFileWatcher temp = null; lock (_lock) { if (_watcher != null) { Log.Info("Will stop watching file: ", _filePath); temp = _watcher; _watcher = null; } } if (temp != null) { temp.Stop(); Log.Info("Stopped the local watcher."); } } catch (Exception ex) { Log.Error(ex); throw; //throw new FaultException<ExceptionDetail>(new ExceptionDetail(ex), "Exception stopping watching file '" + (_filePath ?? "<null>") + "' (on the server)."); } }
// Stop watching the current file, if any, and start // watching the specified file. public void StartWatching2(string filePath, Guid guidForEvents) { using (Log.InfoCall()) { if (string.IsNullOrEmpty(filePath)) { throw new FaultException("The file path to watch is null or empty."); } else if (!filePath.EndsWith(".tx1", StringComparison.OrdinalIgnoreCase)) { throw new FaultException("The file extension of the file to watch must be \".tx1\"."); } else { try { lock (_lock) { StopWatching(); Log.Info("Will start watching file: ", filePath); _filePath = filePath; _watcher = new LocalFileWatcher(filePath, guidForEvents); // When the file changes, we'll notify the client via this callback object. _callback = OperationContext.Current.GetCallbackChannel <IFileMonitorCallback>(); (_callback as ICommunicationObject).Closing += (sender, e) => Log.Debug("Callback channel is closing."); (_callback as ICommunicationObject).Closed += (sender, e) => Log.Debug("Callback channel is closed."); (_callback as ICommunicationObject).Faulted += (sender, e) => Log.Debug("Callback channel is faulted."); _watcher.Changed += _watcher_Changed; _watcher.Renamed += _watcher_Renamed; } } catch (Exception ex) { Log.Error(ex); throw; //throw new FaultException<ExceptionDetail>(new ExceptionDetail(ex), "Exception watching file '" + (filePath ?? "<null>") + "' (on the server)."); } } } }