Пример #1
0
        public async Task StartAsync(CancellationToken cancellationToken)
        {
            try
            {
                await _service.StartAsync(cancellationToken);

                _logger.LogInformation($"Service {_service.GetType().FullName} started");
            }
            catch (Exception e)
            {
                _logger.LogCritical(e, $"Error starting hosted service {_service.GetType().FullName}");
                _applicationLifetime.StopApplication();
            }
        }
Пример #2
0
        public void RunService()
        {
            log.Trace(() => $"Going to run a service of type {service.GetType().FullName}");

            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            AppDomain.CurrentDomain.DomainUnload       += CurrentDomain_DomainUnload;

            log.Trace(() => $"Registered Unhandled Exception Event");
            handle = new AutoResetEvent(false);

            Console.CancelKeyPress += Console_CancelKeyPress;


            service.ServiceFailed += Service_ServiceFailed;
            service.RunService();


            Console.WriteLine("Application Running. Use Cancel Keys (normally Ctrl+C) to close");

            log.Info("Service Started, about to block main thread");

            handle.WaitOne();
            log.Info("Main Thread Resumed to Shutdown");
            service.Shutdown();
            log.Info("Service Shutdown. Goodbye!");
        }
Пример #3
0
        /// <summary>
        /// Adds the IHostedService to the service collection.
        /// </summary>
        /// <param name="serviceBuilder">A collection of services</param>
        /// <param name="service">The IHostedService to add to the service collection.</param>
        /// <returns>List of services with the IHostedService attached.</returns>
        public static IServiceCollection AddHostedService(this IServiceCollection serviceBuilder, IHostedService service)
        {
            var type = service.GetType();

            ProcessTypes.Add(type);
            serviceBuilder.AddSingleton(type, service);
            return(serviceBuilder);
        }
Пример #4
0
        /// <summary>
        /// Adds the hosted service into the service collection.
        /// </summary>
        /// <param name="service">The service to add.</param>
        /// <returns>AppHostBuilder with the new service added.</returns>
        public AppHostBuilder AddHostedService(IHostedService service)
        {
            var type = service.GetType();

            _services.AddSingleton(type, service);
            _processTypes.Add(type);
            return(this);
        }
Пример #5
0
        private static Task ExecuteAsync(IHostedService hostedService, CancellationToken cancellationToken)
        {
            //Microsoft.Extensions.Hosting.BackgroundService.ExecuteAsync

            Type       type = hostedService.GetType();
            MethodInfo mi   = type.GetMethod("ExecuteAsync", BindingFlags.NonPublic | BindingFlags.Instance);

            object value = mi.Invoke(hostedService, new object[] { cancellationToken });
            var    task  = value as Task;

            return(task);
        }
Пример #6
0
        public void Register <T>(IHostedService service, string friendlyName) where T : class, IHostedService
        {
            if (typeof(T) != service.GetType())
            {
                throw new ArgumentException($"Type mismatch: {nameof(T)} is {typeof(T).Name}, but {nameof(service)} is {service.GetType()}.");
            }

            if (IsStartAllAsyncStarted)
            {
                throw new InvalidOperationException("Services are already started.");
            }

            lock (ServicesLock)
            {
                if (AnyNoLock <T>())
                {
                    throw new InvalidOperationException($"{typeof(T).Name} is already registered.");
                }
                Services.Add(new HostedService(service, friendlyName));
            }
        }
Пример #7
0
		public Guid Install(IHostedService service)
		{
			this.WriteDebugMessage(string.Format("Installing hosted service {0}({1}).", service.GetType().Name, service.Name));

			var serviceId = Guid.NewGuid();

			var cancellationTokenSource = new CancellationTokenSource();
			var cancellationToken = cancellationTokenSource.Token;

			var task = createTask(service, cancellationToken);

			_taskMap.Add(serviceId, task);

			_taskTokenSources.Add(serviceId, cancellationTokenSource);

			Services.Add(serviceId, service);

			this.WriteInfoMessage(string.Format("Installed hosted service {0}.", service.Name));

			return serviceId;
		}
Пример #8
0
 internal HostedServiceWrapper(IHostedService inner, ILogger <HostedServiceWrapper> logger)
 {
     _inner  = inner;
     _logger = logger;
     Name    = inner.GetType().Name.Replace("HostedService", "");
 }
Пример #9
0
 private void Service_ServiceFailed(IHostedService service)
 {
     log.Debug(service.GetType().FullName + " Failed. Stopping Service Host");
     Shutdown();
 }
Пример #10
0
 public LoggingServiceDecorator(ILoggerFactory loggerFactory, IHostedService inner)
 {
     this.inner  = inner;
     this.logger = loggerFactory.CreateLogger(inner.GetType());
 }
Пример #11
0
 private static async Task StartServiceAsync(IHostedService service)
 {
     Console.WriteLine($"Starting {service.GetType().Name}.");
     await service.StartAsync(default);