private void Start() { // This class (RemoteFileWatcher) is both the client and the callback object. _service = new ProxyFileMonitor(this); // Must come before assigning the event handlers. Log.Debug("Setting host to ", _hostAndPort); _service.SetHost(_hostAndPort); _service.SetCredentials(_creds); _service.InnerChannel.Opening += (sender, e) => Log.Debug("RemoteFileWatcher opening."); _service.InnerChannel.Opened += (sender, e) => Log.Debug("RemoteFileWatcher opened."); _service.InnerChannel.Closing += (sender, e) => Log.Debug("RemoteFileWatcher closing."); _service.InnerChannel.Closed += (sender, e) => Log.Debug("RemoteFileWatcher closed."); Log.Debug("Calling StartWatching() for file ", _path); if (_guidForEvents == Guid.Empty) { _service.StartWatching(_path);// This will throw an exception if the server is offline, the service is not running, etc. } else { _service.StartWatching2(_path, _guidForEvents);// This will throw an exception if the server is offline, the service is not running, etc. } // We established a connection, now watch for it to fault. _service.InnerChannel.Faulted += InnerChannel_Faulted; }
public void Stop() { using (Log.DebugCall()) { lock (_lock) { if (_service != null) { // Since we're stopping we don't need to handle the Faulted event any more. // Also if we continue to handle the Faulted event it can cause recursion // and try to restart the connection if it gets raised in here. _service.InnerChannel.Faulted -= InnerChannel_Faulted; if (_service.State == CommunicationState.Opened) { try { Log.Debug("Calling _service.StopWatching()"); _service.StopWatching(); // Can raise the Faulted event. } catch (Exception ex) { // Most likely, the proxy is faulted. Log.Debug(ex); } } // We have our own implementation of Dispose() that's // safe to call even if the proxy is faulted. Log.Debug("Calling _service.Dispose()"); _service.Dispose(); _service = null; } } } }