/// <summary> /// Периодический вызов метода контракта. /// </summary> /// <typeparam name="TContract">Тип контракта.</typeparam> /// <param name="service">Экземпляр для работы с сервисом.</param> /// <param name="execute">При возникновении события о вызове.</param> /// <param name="period">Период вызова.</param> protected void PeriodicExecutor <TContract>(TContract service, Func <TContract, int, bool> execute, int period = 3000) { Task.Factory.StartNew( () => { int call = 0; while (true) { try { if (!execute(service, ++call)) { return; } } catch (Exception ex) { SysConsole.WriteErrorLine(ex.ToString()); } Thread.Sleep(period); } }); }
/// <summary> /// Вывод текста ожидания клиента. /// </summary> public static void AwaitingClientConnections() { SysConsole.WriteLine(); SysConsole.WriteQuestionLine("Awaiting client connections..."); Console.ReadKey(); }
/// <summary> /// Получить настройки пользователя. /// </summary> /// <returns>Экземпляр <see cref="UserSettings"/>.</returns> public UserSettings GetSettings() { var appSide = QuestionManager.Choose( new[] { AppSide.Client, AppSide.Server }, side => side.ToString(), "Choose application type:"); var bindingElement = QuestionManager.Choose( GetBindings().ToArray(), element => $"[{GetBindingType(element).Name}] Name [{element.Name}]", "Select binding name:"); var bindingType = GetBindingType(bindingElement); var binding = (Binding)Activator.CreateInstance(bindingType, bindingElement.Name); string serviceAddress = null; if (appSide == AppSide.Client) { const string NewServiceAddress = "I wanna type a new address"; serviceAddress = NewServiceAddress; var recentAddresses = new [] { NewServiceAddress }.Union(ReadRecentAddresses()).ToArray(); if (recentAddresses.Length > 1) { serviceAddress = QuestionManager.Choose( recentAddresses, s => s, // Выберите адрес сервера из ранее использованных: "Select presented before address:", // Все параметры содержатся в app.config файле "Its presented in app.config"); } if (serviceAddress == NewServiceAddress) { do { // Введите новый адрес компьютера serviceAddress = QuestionManager.Read("Please type service host address (ex: localhost):"); using (var ping = new Ping()) { try { var pingResult = ping.Send(serviceAddress); if (pingResult.Status != IPStatus.Success) { serviceAddress = null; SysConsole.WriteErrorLine( $"Ping status [{pingResult.Status}]. Reply from {pingResult.Address}: bytes={pingResult.Buffer.Length} time={pingResult.RoundtripTime}ms"); } } catch (PingException exception) { serviceAddress = null; SysConsole.WriteErrorLine(exception.ToString()); } } } while (string.IsNullOrEmpty(serviceAddress)); SaveRecentAddress(serviceAddress); } } else if (appSide == AppSide.Server) { serviceAddress = UserSettings.DefaultServiceListenAddress; } var serviceHost = new Uri($"{binding.Scheme}://{serviceAddress}:{UserSettings.DefaultServicePort}"); return(new UserSettings(binding, appSide, serviceHost)); }