public void Start(CommandSettings commandSettings, bool saveCredentials, bool checkPreviousBackup) { // argument checks if (commandSettings == null) { throw new ArgumentNullException(nameof(commandSettings)); } SystemSettingsSwitcherSettings systemSettingsSwitcherSettings = commandSettings.SystemSettingsSwitcher; if (systemSettingsSwitcherSettings == null) { throw new ArgumentNullException(nameof(commandSettings.SystemSettingsSwitcher)); } ProxySettings proxySettings = commandSettings.Proxy; if (proxySettings == null) { throw new ArgumentNullException(nameof(commandSettings.Proxy)); } // state checks if (this.proxy != null) { throw new InvalidOperationException("The proxy is already started."); } Debug.Assert(this.switcher == null); Debug.Assert(this.backup == null); try { ComponentFactory componentFactory = this.Owner.ComponentFactory; // check the state of previous backup if (checkPreviousBackup) { this.Owner.CheckPreviousBackup(); } // create a system settings swither SystemSettingsSwitcher switcher = componentFactory.CreateSystemSettingsSwitcher(this.Owner, systemSettingsSwitcherSettings); this.switcher = switcher; // setup credential dictionary lock (this.credentialsLocker) { IEnumerable <CredentialSettings> credentials = commandSettings.Credentials; this.dictionary = (credentials == null)? new Dictionary <string, CredentialSettings>(): credentials.ToDictionary(c => c.EndPoint); this.isCredentialsDirty = false; } // create a proxy Proxy proxy = componentFactory.CreateProxy(proxySettings); this.proxy = proxy; // detect the current proxy IActualProxy actualProxy = switcher.GetActualProxy(); if (actualProxy == null) { // no actual proxy to which it connects throw new Exception(Resources.CommandBase_Message_NoActualProxy); } try { // log CommandBase owner = this.Owner; if (owner.ShouldLog(TraceEventType.Verbose)) { // log the actual proxy owner.LogVerbose($"ActualProxy is '{actualProxy.Description}'"); // log whether system settings is being switched string label = switcher.Enabled ? "enabled" : "disabled"; owner.LogVerbose($"SystemSettingsSwitch: {label}"); } // test actual proxy if (switcher.TestWebProxy(actualProxy) == false) { string message = string.Format(Resources.SystemSettingsSwitcher_Message_ProxyIsNotConnectable, actualProxy.Description); throw new Exception(message); } // start the proxy proxy.ActualProxy = actualProxy; actualProxy = null; // its ownership has been moved to the proxy object. proxy.Start(this); this.saveCredentials = saveCredentials; // switch system settings this.backup = switcher.Switch(proxy); if (this.backup != null) { // save backup settings owner.SaveSystemSettingsBackup(this.backup); } this.commandSettings = commandSettings; } catch { DisposableUtil.ClearDisposableObject(ref actualProxy); throw; } } catch { Stop(systemSessionEnding: false); throw; } return; }