public async Task Add <T>() where T : IService, new() { try { string serviceName = GetServiceName <T>(); Log.Write($"Adding service: {serviceName}", "Services"); IService service = Activator.CreateInstance <T>(); this.services.Add(service); OnServiceInitializing?.Invoke(serviceName); await service.Initialize(this); // If we've already started, and this service is being added late (possibly by a module from its start method) start the service immediately. if (this.IsStarted) { OnServiceStarting?.Invoke(serviceName); await service.Start(); } } catch (Exception ex) { Log.Write(new Exception($"Failed to initialize service: {typeof(T).Name}", ex)); } }
public async Task Add <T>() where T : IService, new() { try { var sw = new Stopwatch(); sw.Start(); var serviceName = GetServiceName <T>(); var service = Activator.CreateInstance <T>(); this.services.Add(service); OnServiceInitializing?.Invoke(serviceName); await service.Initialize(); if (this.IsStarted) { OnServiceStarting?.Invoke(serviceName); await service.Start(); } Log.Write($"Added service: {serviceName} in {sw.ElapsedMilliseconds}ms", "Services"); } catch (Exception ex) { Log.Write(new Exception($"Failed to initialize service: {typeof(T).Name}", ex)); } }