Ejemplo n.º 1
0
        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));
            }
        }
        public async Task StartServices()
        {
            var services = new List <IService>(this.services);

            services.Reverse();
            foreach (var service in services)
            {
                OnServiceStarting?.Invoke(GetServiceName(service.GetType()));
                await service.Start();
            }
        }
Ejemplo n.º 4
0
        public async Task StartServices()
        {
            // Since starting a service _can_ add new services, copy the list first.
            List <IService> services = new List <IService>(this.services);

            foreach (IService service in services)
            {
                OnServiceStarting?.Invoke(GetServiceName(service.GetType()));
                await service.Start();
            }

            this.IsStarted = true;
            Log.Write($"Services Started", "Services");
        }
Ejemplo n.º 5
0
        public async Task StartServices()
        {
            Stopwatch sw = new Stopwatch();

            // Since starting a service _can_ add new services, copy the list first.
            List <IService> services = new List <IService>(Services);

            services.Reverse();
            foreach (IService service in services)
            {
                string name = GetServiceName(service.GetType());
                sw.Restart();
                OnServiceStarting?.Invoke(name);
                await service.Start();

                Log.Write($"Started Service: {name} in {sw.ElapsedMilliseconds}ms", "Services");
            }

            IsStarted = true;
            Log.Write($"Services Started", "Services");
        }