Example #1
0
        private void HandlePosixSignal(PosixSignalContext context)
        {
            Debug.Assert(context.Signal == PosixSignal.SIGINT || context.Signal == PosixSignal.SIGQUIT || context.Signal == PosixSignal.SIGTERM);

            context.Cancel = true;
            ApplicationLifetime.StopApplication();
        }
Example #2
0
 internal Host(IServiceProvider services)
 {
     Services             = services ?? throw new ArgumentNullException(nameof(services));
     _applicationLifetime = Services.GetRequiredService <IApplicationLifetime>() as ApplicationLifetime;
     _logger       = Services.GetRequiredService <ILogger <Host> >();
     _hostLifetime = Services.GetRequiredService <IHostLifetime>();
 }
Example #3
0
        public Host(IServiceProvider services,
                    IHostEnvironment hostEnvironment,
                    PhysicalFileProvider defaultProvider,
                    IHostApplicationLifetime applicationLifetime,
                    ILogger <Host> logger,
                    IHostLifetime hostLifetime,
                    IOptions <HostOptions> options)
        {
            ThrowHelper.ThrowIfNull(services);
            ThrowHelper.ThrowIfNull(applicationLifetime);
            ThrowHelper.ThrowIfNull(logger);
            ThrowHelper.ThrowIfNull(hostLifetime);

            Services             = services;
            _applicationLifetime = (applicationLifetime as ApplicationLifetime) !;
            _hostEnvironment     = hostEnvironment;
            _defaultProvider     = defaultProvider;

            if (_applicationLifetime is null)
            {
                throw new ArgumentException(SR.IHostApplicationLifetimeReplacementNotSupported, nameof(applicationLifetime));
            }
            _logger       = logger;
            _hostLifetime = hostLifetime;
            _options      = options?.Value ?? throw new ArgumentNullException(nameof(options));
        }
        public Task WaitForStartAsync(CancellationToken cancellationToken)
        {
            if (!Options.SuppressStatusMessages)
            {
                ApplicationLifetime.ApplicationStarted.Register(() =>
                {
                    Logger.LogInformation("Application started. Press Ctrl+C to shut down.");
                    Logger.LogInformation("Hosting environment: {envName}", Environment.EnvironmentName);
                    Logger.LogInformation("Content root path: {contentRoot}", Environment.ContentRootPath);
                });
            }

            AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) =>
            {
                ApplicationLifetime.StopApplication();
                _shutdownBlock.WaitOne();
            };
            Console.CancelKeyPress += (sender, e) =>
            {
                e.Cancel = true;
                ApplicationLifetime.StopApplication();
            };

            // Console applications start immediately.
            return(Task.CompletedTask);
        }
Example #5
0
 private void OnProcessExit(object sender, EventArgs e)
 {
     ApplicationLifetime.StopApplication();
     _shutdownBlock.WaitOne();
     // On Linux if the shutdown is triggered by SIGTERM then that's signaled with the 143 exit code.
     // Suppress that since we shut down gracefully. https://github.com/aspnet/AspNetCore/issues/6526
     System.Environment.ExitCode = 0;
 }
Example #6
0
 public Host(IServiceProvider services, IApplicationLifetime applicationLifetime, ILogger <Host> logger,
             IHostLifetime hostLifetime, IOptions <HostOptions> options)
 {
     Services             = services ?? throw new ArgumentNullException(nameof(services));
     _applicationLifetime = (applicationLifetime ?? throw new ArgumentNullException(nameof(applicationLifetime))) as ApplicationLifetime;
     _logger       = logger ?? throw new ArgumentNullException(nameof(logger));
     _hostLifetime = hostLifetime ?? throw new ArgumentNullException(nameof(hostLifetime));
     _options      = options?.Value ?? throw new ArgumentNullException(nameof(options));
 }
Example #7
0
        private void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            e.Cancel = true;
            ApplicationLifetime.StopApplication();

            // Don't block in process shutdown for CTRL+C/SIGINT since we can set e.Cancel to true
            // we assume that application code will unwind once StopApplication signals the token
            _shutdownBlock.Set();
        }
Example #8
0
 private void OnProcessExit(object sender, EventArgs e)
 {
     ApplicationLifetime.StopApplication();
     if (!_shutdownBlock.WaitOne(HostOptions.ShutdownTimeout))
     {
         Logger.LogInformation("Waiting for the host to be disposed. Ensure all 'IHost' instances are wrapped in 'using' blocks.");
     }
     _shutdownBlock.WaitOne();
     // On Linux if the shutdown is triggered by SIGTERM then that's signaled with the 143 exit code.
     // Suppress that since we shut down gracefully. https://github.com/dotnet/aspnetcore/issues/6526
     System.Environment.ExitCode = 0;
 }
Example #9
0
 public Host(IServiceProvider services, IHostApplicationLifetime applicationLifetime, ILogger <Host> logger,
             IHostLifetime hostLifetime, IOptions <HostOptions> options)
 {
     Services             = services ?? throw new ArgumentNullException(nameof(services));
     _applicationLifetime = (applicationLifetime ?? throw new ArgumentNullException(nameof(applicationLifetime))) as ApplicationLifetime;
     if (_applicationLifetime is null)
     {
         throw new ArgumentException("Replacing IHostApplicationLifetime is not supported.", nameof(applicationLifetime));
     }
     _logger       = logger ?? throw new ArgumentNullException(nameof(logger));
     _hostLifetime = hostLifetime ?? throw new ArgumentNullException(nameof(hostLifetime));
     _options      = options?.Value ?? throw new ArgumentNullException(nameof(options));
 }
Example #10
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);
        }
Example #11
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();
        }
Example #12
0
 private void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
 {
     e.Cancel = true;
     ApplicationLifetime.StopApplication();
 }
Example #13
0
        public async Task HostShutdownFiresApplicationLifetimeStoppedBeforeHostLifetimeStopped()
        {
            var stoppingCalls  = 0;
            var startedCalls   = 0;
            var disposingCalls = 0;

            FakeHostLifetime    fakeHostLifetime    = null;
            ApplicationLifetime applicationLifetime = null;

            using (var host = CreateBuilder()
                              .ConfigureServices((services) =>
            {
                Action started = () =>
                {
                    startedCalls++;
                };

                Action stopping = () =>
                {
                    stoppingCalls++;
                };

                Action disposing = () =>
                {
                    disposingCalls++;
                };

                services.AddSingleton <IHostedService>(_ => new DelegateHostedService(started, stopping, disposing));

                services.AddSingleton <IHostLifetime>(_ =>
                {
                    fakeHostLifetime = new FakeHostLifetime();

                    fakeHostLifetime.StopAction = () =>
                    {
                        Assert.Equal(1, startedCalls);
                        Assert.Equal(1, stoppingCalls);
                        Assert.True(applicationLifetime.ApplicationStopped.IsCancellationRequested);
                    };
                    return(fakeHostLifetime);
                }
                                                      );
            })
                              .Build())
            {
                var lifetime     = host.Services.GetRequiredService <IHostApplicationLifetime>();
                var hostLifetime = host.Services.GetRequiredService <IHostLifetime>();
                applicationLifetime = lifetime as ApplicationLifetime;
                Assert.NotNull(applicationLifetime);

                Assert.Equal(0, startedCalls);

                await host.StartAsync();

                Assert.Equal(1, startedCalls);
                Assert.Equal(0, stoppingCalls);
                Assert.Equal(0, disposingCalls);

                Assert.True(lifetime.ApplicationStarted.IsCancellationRequested);
                Assert.False(lifetime.ApplicationStopping.IsCancellationRequested);
                Assert.False(lifetime.ApplicationStopped.IsCancellationRequested);

                await host.StopAsync();

                Assert.True(lifetime.ApplicationStopping.IsCancellationRequested);
                Assert.True(lifetime.ApplicationStopped.IsCancellationRequested);
                Assert.Equal(1, startedCalls);
                Assert.Equal(1, stoppingCalls);
            }
        }
Example #14
0
 private void OnProcessExit(object sender, EventArgs e)
 {
     ApplicationLifetime.StopApplication();
     _shutdownBlock.WaitOne();
 }