/// <summary> /// Connect a service to the bot file. /// </summary> /// <param name="newService"><see cref="ConnectedService"/> to add.</param> public void ConnectService(ConnectedService newService) { if (newService == null) { throw new ArgumentNullException(nameof(newService)); } if (string.IsNullOrEmpty(newService.Id)) { int maxValue = 0; foreach (var service in this.Services) { if (int.TryParse(service.Id, out int id) && id > maxValue) { maxValue = id; } } newService.Id = (++maxValue).ToString(); } else if (this.Services.Where(s => s.Type == newService.Type && s.Id == newService.Id).Any()) { throw new Exception($"service with {newService.Id} is already connected"); } this.Services.Add(newService); }
/// <summary> /// connect a service to the bot file. /// </summary> /// <param name="newService">sevice to add</param> public void ConnectService(ConnectedService newService) { if (this.Services.Where(s => s.Type == newService.Type && s.Id == newService.Id).Any()) { throw new Exception($"service with {newService.Id} is already connected"); } else { // give unique name var nameCount = 1; var name = newService.Name; while (true) { if (nameCount > 1) { name = $"{newService.Name} ({nameCount})"; } if (!this.Services.Where(s => s.Name == name).Any()) { break; } nameCount++; } newService.Name = name; this.Services.Add(newService); } }
/// <summary> /// Connect a service to the bot file. /// </summary> /// <param name="newService"><see cref="ConnectedService"/> to add.</param> public void ConnectService(ConnectedService newService) { if (newService == null) { throw new ArgumentNullException(nameof(newService)); } if (string.IsNullOrEmpty(newService.Id)) { int maxValue = 0; foreach (var service in this.Services) { if (int.TryParse(service.Id, out int id) && id > maxValue) { maxValue = id; } } #pragma warning disable CA1305 // Specify IFormatProvider (this class is obsolete, we won't fix it) newService.Id = (++maxValue).ToString(); #pragma warning restore CA1305 // Specify IFormatProvider } else if (this.Services.Where(s => s.Type == newService.Type && s.Id == newService.Id).Any()) { throw new InvalidOperationException($"service with {newService.Id} is already connected"); } this.Services.Add(newService); }
/// <summary> /// connect a service to the bot file. /// </summary> /// <param name="newService">sevice to add</param> public void ConnectService(ConnectedService newService) { if (this.Services.Where(s => s.Type == newService.Type && s.Id == newService.Id).Any()) { throw new Exception($"service with {newService.Id} is already connected"); } else { // assign a unique random id between 0-255 (255 services seems like a LOT of services var rnd = new Random(); do { newService.Id = rnd.Next(byte.MaxValue).ToString(); }while (this.Services.Where(s => s.Id == newService.Id).Any()); this.Services.Add(newService); } }