[英]Kestrel shutdown function in Startup.cs in ASP.NET Core
Is there a shutdown function when using Microsoft.AspNet.Server.Kestrel
?使用
Microsoft.AspNet.Server.Kestrel
时是否有关机 function ? ASP.NET Core
(formerly ASP.NET vNext
) clearly has a Startup sequence, but no mention of shutdown sequence and how to handle clean closure. ASP.NET Core
(以前ASP.NET vNext
)显然有一个启动顺序,但没有提到关闭顺序以及如何处理干净关闭。
In ASP.NET Core you can register to the cancellation tokens provided by IApplicationLifetime
在 ASP.NET Core 中,您可以注册到
IApplicationLifetime
提供的取消令牌
public class Startup
{
public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime)
{
applicationLifetime.ApplicationStopping.Register(OnShutdown);
}
private void OnShutdown()
{
// Do your cleanup here
}
}
IApplicationLifetime
is also exposing cancellation tokens for ApplicationStopped
and ApplicationStarted
as well as a StopApplication()
method to stop the application. IApplicationLifetime
还公开了ApplicationStopped
和ApplicationStarted
的取消令牌以及用于停止应用程序的StopApplication()
方法。
From comments @Horkrine来自@Horkrine 的评论
For .NET Core 3.0+ it is recommended to use IHostApplicationLifetime instead, as IApplicationLifetime will be deprecated soon.
对于 .NET Core 3.0+,建议改用 IHostApplicationLifetime,因为 IApplicationLifetime 很快就会被弃用。 The rest will still work as written above with the new service
其余的仍将按照上面所写的方式与新服务一起工作
In addition to the original answer, I had an error while trying to wire the IApplicationLifetime within the constructor.除了原始答案之外,我在尝试在构造函数中连接 IApplicationLifetime 时遇到错误。
I solved this by doing:我通过这样做解决了这个问题:
public class Startup
{
public void Configure(IApplicationBuilder app)
{
var applicationLifetime = app.ApplicationServices.GetRequiredService<IApplicationLifetime>();
applicationLifetime.ApplicationStopping.Register(OnShutdown);
}
private void OnShutdown()
{
// Do your cleanup here
}
}
I solved it with the application lifetime callback events我用应用程序生命周期回调事件解决了它
Startup.cs启动.cs
public void Configure(IHostApplicationLifetime appLifetime) {
appLifetime.ApplicationStarted.Register(() => {
Console.WriteLine("Press Ctrl+C to shut down.");
});
appLifetime.ApplicationStopped.Register(() => {
Console.WriteLine("Terminating application...");
System.Diagnostics.Process.GetCurrentProcess().Kill();
});
}
Program.cs程序.cs
Also, use UseConsoleLifetime()
while building the host.此外,在构建主机时使用
UseConsoleLifetime()
。
Host.CreateDefaultBuilder(args).UseConsoleLifetime(opts => opts.SuppressStatusMessages = true);
When using .NET 6 without a Startup class, you can access the ApplicationStopping
CancellationToken
via the built web application in Program.cs, eg当使用没有 Startup 类的 .NET 6 时,您可以通过 Program.cs 中构建的 Web 应用程序访问
ApplicationStopping
CancellationToken
,例如
var app = builder.Build();
...
app.Lifetime.ApplicationStopping.Register(() => ...);
...
app.Run();
In.Net 6 if you are not using Startup.cs. In.Net 6 如果您不使用 Startup.cs。 You could use ApplicationStopping method in Program.cs
您可以在 Program.cs 中使用 ApplicationStopping 方法
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
ContentRootPath = Directory.GetCurrentDirectory(),
WebRootPath = "wwwroot"
});
var app = builder.Build();
app.Lifetime.ApplicationStopping.Register(() =>
{
// Do Something
});
app.Run();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.