public async Task StartConnecting(ILifetimeScope lifetimeScope) { var uris = new List <Uri>(); foreach (var serverUri in _options.ServerUris) { if (Uri.TryCreate(serverUri, UriKind.Absolute, out var uri)) { uris.Add(uri); } else { Logger.Warn("Unable to parse uri {uri}", serverUri); } } while (true) { foreach (var serverUri in uris) { Logger.Debug("Try to connect to {uri}", serverUri); try { var connection = await _serverConnector.TryConnect(serverUri); Logger.Debug("Connection succeeded, download modules"); await _moduleDownloader.Load(connection.PackagesLock, connection, CancellationToken.None); Logger.Debug("Modules downloaded successfully, loading configuration"); var configurationInfo = await ClientConfigurationResource.GetConfigurationsInfo(connection.RestClient); if (!_configurationManager.IsSynchronized(configurationInfo)) { var configurations = await ClientConfigurationResource.GetConfigurations(connection.RestClient); _configurationManager.Synchronize(configurations, connection.PackagesLock); } if (!_packageLockUpdater.Verify(connection.PackagesLock)) { _processController.Restart(); return; } var loadedContext = await _packageLockLoader.Load(connection.PackagesLock); var controllers = await loadedContext.GetControllers(); var restClientWrapper = lifetimeScope.Resolve <MazeRestClientWrapper>(); restClientWrapper.Initialize(connection.RestClient); CurrentConnectionScope = lifetimeScope.BeginLifetimeScope(builder => { if (loadedContext.PackagesLoaded) { loadedContext.Configure(builder); } builder.RegisterMazeServices(cache => cache.BuildCache(controllers)); }); CurrentConnection = connection; using (CurrentConnectionScope) { await CurrentConnectionScope.Execute <IConnectedAction>(); await connection.InitializeWebSocket(CurrentConnectionScope); } CurrentConnection = null; restClientWrapper.Initialize(null); break; } catch (Exception e) { Logger.Warn(e, "Error occurred when trying to connect to {uri}", serverUri); } } await Task.Delay(_options.ReconnectDelay); } }