使用Microsoft.AspNet.Server.Kestrel
时是否有关闭功能? ASP.NET Core(以前称为ASP.NET vNext)显然具有启动顺序,但没有提及关闭顺序以及如何处理清理关闭。ASP.NET Core中的Startup.cs中的Kestrel关机功能
18
A
回答
23
在ASP.NET核心可以通过IApplicationLifetime
public class Startup
{
public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime)
{
applicationLifetime.ApplicationStopping.Register(OnShutdown);
}
private void OnShutdown()
{
// Do your cleanup here
}
}
IApplicationLifetime
所提供的取消标记寄存器也被暴露取消标记为ApplicationStopped
和ApplicationStarted
以及一个StopApplication()
方法来停止该应用程序。
9
除了原来的答案,我试图在构造函数中连接IApplicationLifetime时出错。
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
}
}
+1
当您从main手动配置虚拟主机时,这种方式没有使用启动类。 –
相关问题
- 1. 为HTTPS配置ASP.NET Core 2.0 Kestrel
- 2. Startup.cs在自托管的.NET Core Console应用程序中
- 3. 有没有办法在Kestrel/ASP.NET Core中获取请求队列中的时间?
- 4. 如何正确注入startup.cs的ConfigureServices方法中的DbContext实例(ASP.net core 1.1)?
- 5. 请求内容的意外结束Kestrel ASP.NET Core
- 6. ASP.NET中的Owin Startup.cs IIS托管项目
- 7. 在Visual Studio中的ASP.NET Core(ASP.NET 5)中重新启动web命令重启Kestrel(ASP.NET 5)
- 8. 调用C#中的预关机功能
- 9. 如何关闭ASP.NET Core中的camelCasing?
- 10. 在ASP.NET MVC Core项目的View中使用C#7功能
- 11. ASP.NET Core中的MD5CryptoServiceProvider
- 12. ASP.NET Core中的ServicePointManager
- 13. ASP.NET Core中的AntiForgeryConfig.UniqueClaimTypeIdentifier
- 14. 使用Ionic访问ASP.NET Core功能
- 15. Kestrel和ASP.NET Core MVC使用自定义基本路径
- 16. 在Startup.cs中生成角色在.NET Core 2.0中不起作用
- 17. 如何在ASP.NET Core MVC中完成此功能?
- 18. asp.net中的后台功能
- 19. 在关机功能中捕捉异常
- 20. 在asp.net中使用Startup.cs中的ConfigureServices和Configure方法vnext
- 21. ASP.NET中的HttpContext.Current.Request.Form.AllKeys CORE版本
- 22. ASP.NET Core MVC中的ValueTuple
- 23. ASP.NET Core中的System.Data.Entity.Spatial替换
- 24. 模型中的ASP.NET CORE DI
- 25. ASP.NET Core 1.0中的package.json
- 26. ASP.NET Core 1.0中的Dropdownmenu
- 27. ASP.NET/ASP.NET MVC中的类SSI功能
- 28. 如何在startup.cs中创建依赖于构造函数的类的实例(ASP.NET Core)
- 29. 关机前执行功能
- 30. 退出关机功能python
难道这所有的操作系统和服务器一起工作:
我这样做解决了这个?我一直无法在OSX + Kestrel上工作。 – opiethehokie
它应该可以在所有平台上工作,否则它可能是一个错误。 github上有一些旧问题,也许你可以在https://github.com/aspnet/Hosting/issues/151上尝试davidfowl的解决方法,否则请尝试在https://github.com/aspnet/Hosting/issues处打开一个问题。一个错误,它仍然在RC1准备RC2 – Tseng
@opiethehokie:也可能与https://github.com/aspnet/Hosting/issues/516有关。它是用于linux的,但据我所知,在MacOS上也使用了SIGINT和SIGTERM。对于知道MacOS发送哪些信号来停止该过程不够熟悉。也许有趣的知道,如果你使用的是dnx核心1.0或全单声道框架 – Tseng