延长应用程序关闭超时
Extending Application Shutdown Timeout
在我的 .NET Core 2.1
应用程序中,我有一个继承自 BackgroundService
class 的服务,该服务在新线程上启动了一个 long-运行 服务。然后,此服务会命中一个范围内的服务,该服务从数据库中读取并发送一些电子邮件。我正在尝试测试我对应用程序正常关闭的处理,但 运行 在我的测试场景中遇到了一个奇怪的问题。
阅读 BackgroundService 的 MSDN 博客条目后,提到:
"By default, the cancellation token is set with a 5 second timeout, although you can change that value when building your WebHost using the UseShutdownTimeout extension of the IWebHostBuilder. This means that our service is expected to cancel within 5 seconds otherwise it will be more abruptly killed."
The following code would be changing that time to 10 seconds.
WebHost.CreateDefaultBuilder(args)
.UseShutdownTimeout(TimeSpan.FromSeconds(10))
...
在我的 Program.cs
文件中,我已经实现了这个扩展方法(并且也尝试使用 UseSettings
方法)来尝试延长我的超时关闭,如下所示:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog()
//.UseSetting(WebHostDefaults.ShutdownTimeoutKey, "20")
.UseShutdownTimeout(TimeSpan.FromSeconds(20))
.Build();
我在我的作用域服务中有一个断点,我试图通过设置 10 秒的等待时间并关闭控制台以在线程等待时启动关闭来测试此超时的添加。然而,我发现在启动关机 5 秒后,应用程序被终止,就好像它仍在使用默认的 5 秒关机而不是新的 20 秒。这种行为的原因可能是什么?
Log.Information("preparing to sleep thread for 10 seconds");
//Thread.Sleep(10000);
await Task.Delay(TimeSpan.FromSeconds(10));
Log.Information("thread awakened");
我的特定 ASP.NET 控制台应用程序没有 web.config 文件,因此我创建了其中一个文件,然后将 shutdownTimeLimit
属性 添加到 aspNetCore
模块部分。
<aspNetCore shutdownTimeLimit="20" processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false">
在我的 .NET Core 2.1
应用程序中,我有一个继承自 BackgroundService
class 的服务,该服务在新线程上启动了一个 long-运行 服务。然后,此服务会命中一个范围内的服务,该服务从数据库中读取并发送一些电子邮件。我正在尝试测试我对应用程序正常关闭的处理,但 运行 在我的测试场景中遇到了一个奇怪的问题。
阅读 BackgroundService 的 MSDN 博客条目后,提到:
"By default, the cancellation token is set with a 5 second timeout, although you can change that value when building your WebHost using the UseShutdownTimeout extension of the IWebHostBuilder. This means that our service is expected to cancel within 5 seconds otherwise it will be more abruptly killed."
The following code would be changing that time to 10 seconds.
WebHost.CreateDefaultBuilder(args)
.UseShutdownTimeout(TimeSpan.FromSeconds(10))
...
在我的 Program.cs
文件中,我已经实现了这个扩展方法(并且也尝试使用 UseSettings
方法)来尝试延长我的超时关闭,如下所示:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog()
//.UseSetting(WebHostDefaults.ShutdownTimeoutKey, "20")
.UseShutdownTimeout(TimeSpan.FromSeconds(20))
.Build();
我在我的作用域服务中有一个断点,我试图通过设置 10 秒的等待时间并关闭控制台以在线程等待时启动关闭来测试此超时的添加。然而,我发现在启动关机 5 秒后,应用程序被终止,就好像它仍在使用默认的 5 秒关机而不是新的 20 秒。这种行为的原因可能是什么?
Log.Information("preparing to sleep thread for 10 seconds");
//Thread.Sleep(10000);
await Task.Delay(TimeSpan.FromSeconds(10));
Log.Information("thread awakened");
我的特定 ASP.NET 控制台应用程序没有 web.config 文件,因此我创建了其中一个文件,然后将 shutdownTimeLimit
属性 添加到 aspNetCore
模块部分。
<aspNetCore shutdownTimeLimit="20" processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false">