From 993cd9f73d8828e350bbcbed43ad520b87df7c11 Mon Sep 17 00:00:00 2001 From: John Luo Date: Tue, 7 Nov 2017 15:55:31 -0800 Subject: [PATCH] Trigger graceful shutdown on stopping webhost service --- .../WebHostService.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNetCore.Hosting.WindowsServices/WebHostService.cs b/src/Microsoft.AspNetCore.Hosting.WindowsServices/WebHostService.cs index f8121e5dce..f468d05fe3 100644 --- a/src/Microsoft.AspNetCore.Hosting.WindowsServices/WebHostService.cs +++ b/src/Microsoft.AspNetCore.Hosting.WindowsServices/WebHostService.cs @@ -1,6 +1,7 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System; using System.ServiceProcess; using Microsoft.Extensions.DependencyInjection; @@ -20,7 +21,7 @@ namespace Microsoft.AspNetCore.Hosting.WindowsServices /// The configured web host containing the web application to host in the Windows service. public WebHostService(IWebHost host) { - _host = host; + _host = host ?? throw new ArgumentNullException(nameof(host)); } protected sealed override void OnStart(string[] args) @@ -48,8 +49,15 @@ namespace Microsoft.AspNetCore.Hosting.WindowsServices { _stopRequestedByWindows = true; OnStopping(); - _host?.Dispose(); - OnStopped(); + try + { + _host.StopAsync().GetAwaiter().GetResult(); + } + finally + { + _host.Dispose(); + OnStopped(); + } } ///