Exemplo n.º 1
0
        public async Task StartAsync(CancellationToken cancellationToken = default)
        {
            _logger.Starting();

            var delayStart = new TaskCompletionSource <object>();

            cancellationToken.Register(obj => ((TaskCompletionSource <object>)obj).TrySetCanceled(), delayStart);
            _hostLifetime.RegisterDelayStartCallback(obj => ((TaskCompletionSource <object>)obj).TrySetResult(null), delayStart);
            _hostLifetime.RegisterStopCallback(obj => (obj as IApplicationLifetime)?.StopApplication(), _applicationLifetime);

            await delayStart.Task;

            _hostedServices = Services.GetService <IEnumerable <IHostedService> >();

            foreach (var hostedService in _hostedServices)
            {
                // Fire IHostedService.Start
                await hostedService.StartAsync(cancellationToken).ConfigureAwait(false);
            }

            // Fire IApplicationLifetime.Started
            _applicationLifetime?.NotifyStarted();

            _logger.Started();
        }
Exemplo n.º 2
0
        public async Task StartAsync(CancellationToken cancellationToken = default)
        {
            _logger.Starting();

            using var combinedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _applicationLifetime.ApplicationStopping);
            CancellationToken combinedCancellationToken = combinedCancellationTokenSource.Token;

            await _hostLifetime.WaitForStartAsync(combinedCancellationToken).ConfigureAwait(false);

            combinedCancellationToken.ThrowIfCancellationRequested();
            _hostedServices = Services.GetService <IEnumerable <IHostedService> >();

            foreach (IHostedService hostedService in _hostedServices)
            {
                // Fire IHostedService.Start
                await hostedService.StartAsync(combinedCancellationToken).ConfigureAwait(false);

                if (hostedService is BackgroundService backgroundService)
                {
                    _ = TryExecuteBackgroundServiceAsync(backgroundService);
                }
            }

            // Fire IHostApplicationLifetime.Started
            _applicationLifetime.NotifyStarted();

            _logger.Started();
        }
Exemplo n.º 3
0
        public async Task StartAsync(CancellationToken cancellationToken = default)
        {
            #region
            _logger.Starting();

            using var combinedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, _applicationLifetime.ApplicationStopping);
            var combinedCancellationToken = combinedCancellationTokenSource.Token;
            #endregion

            await _hostLifetime.WaitForStartAsync(combinedCancellationToken);

            //combinedCancellationToken.ThrowIfCancellationRequested();
            _hostedServices = Services.GetService <IEnumerable <IHostedService> >();

            foreach (var hostedService in _hostedServices)
            {
                // Fire IHostedService.Start
                await hostedService.StartAsync(combinedCancellationToken).ConfigureAwait(false);
            }

            #region
            // Fire IHostApplicationLifetime.Started
            _applicationLifetime?.NotifyStarted();

            _logger.Started();
            #endregion
        }
Exemplo n.º 4
0
        public async Task StartAsync(CancellationToken cancellationToken = default)
        {
            _logger.Starting();

            await _hostLifetime.WaitForStartAsync(cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();
            _hostedServices = Services.GetService <IEnumerable <IHostedService> >();

            foreach (var hostedService in _hostedServices)
            {
                // Fire IHostedService.Start
                await hostedService.StartAsync(cancellationToken).ConfigureAwait(false);
            }

            // Fire IApplicationLifetime.Started
            _applicationLifetime?.NotifyStarted();

            _logger.Started();
        }
Exemplo n.º 5
0
        public virtual Task StartAsync(CancellationToken cancellationToken)
        {
            _logger = _applicationServices.GetRequiredService <ILogger <Host> >();
            _logger.Starting();

            Initialize();

            _applicationLifetime   = _applicationServices.GetRequiredService <IHostLifetime>() as ApplicationLifetime;
            _hostedServiceExecutor = _applicationServices.GetRequiredService <HostedServiceExecutor>();
            var diagnosticSource = _applicationServices.GetRequiredService <DiagnosticSource>();

            // Fire IApplicationLifetime.Started
            _applicationLifetime?.NotifyStarted();

            // Fire IHostedService.Start
            _hostedServiceExecutor.Start();

            _logger.Started();

            return(Task.CompletedTask);
        }
Exemplo n.º 6
0
        public virtual void Start()
        {
            HostingEventSource.Log.HostStart();
            _logger = _applicationServices.GetRequiredService <ILogger <Host> >();
            _logger.Starting();

            Initialize();

            _applicationLifetime   = _applicationServices.GetRequiredService <IApplicationLifetime>() as ApplicationLifetime;
            _hostedServiceExecutor = _applicationServices.GetRequiredService <HostedServiceExecutor>();
            var diagnosticSource = _applicationServices.GetRequiredService <DiagnosticSource>();

            //var httpContextFactory = _applicationServices.GetRequiredService<IHttpContextFactory>();
            //Server.Start(new HostingApplication(_application, _logger, diagnosticSource, httpContextFactory));

            // Fire IApplicationLifetime.Started
            _applicationLifetime?.NotifyStarted();

            // Fire IHostedService.Start
            _hostedServiceExecutor.Start();

            _logger.Started();
        }