如何实现 asp.net core 安全优雅退出 ?
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                如何实现 asp.net core 安全优雅退出 ?
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                咨詢區(qū)
AppDeveloper
我想問一個老生常談的問題,如何可以保證程序優(yōu)雅的退出,這里用 優(yōu)雅 的目的是因為我想在退出之前做一些小動作。
用戶場景:希望在程序退出之前可以從 Consul 上解注冊, 下面是我的模板代碼。
public?static?IWebHostBuilder?CreateWebHostBuilder(string[]?args)?=>WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((host,?config)?=>{}).UseStartup<Service>();回答區(qū)
Edward:
為了能夠讓程序優(yōu)雅的退出,你可以用 IHostApplicationLifetime 哈,它定義了很多 action 枚舉,比如:啟動前,啟動后,停止前 等等。。。
//?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.Threading;namespace?Microsoft.Extensions.Hosting {///?<summary>///?Allows?consumers?to?be?notified?of?application?lifetime?events.?This?interface?is?not?intended?to?be?user-replaceable.///?</summary>public?interface?IHostApplicationLifetime{///?<summary>///?Triggered?when?the?application?host?has?fully?started.///?</summary>CancellationToken?ApplicationStarted?{?get;?}///?<summary>///?Triggered?when?the?application?host?is?performing?a?graceful?shutdown.///?Shutdown?will?block?until?this?event?completes.///?</summary>CancellationToken?ApplicationStopping?{?get;?}///?<summary>///?Triggered?when?the?application?host?is?performing?a?graceful?shutdown.///?Shutdown?will?block?until?this?event?completes.///?</summary>CancellationToken?ApplicationStopped?{?get;?}///?<summary>///?Requests?termination?of?the?current?application.///?</summary>void?StopApplication();} }接下來就可以在相關枚舉上注冊事件啦,參考如下代碼:
public?static?void?Main(string[]?args) {var?host?=?CreateHostBuilder(args).Build();var?life?=?host.Services.GetRequiredService<IHostApplicationLifetime>();life.ApplicationStopped.Register(()?=>?{Console.WriteLine("Application?is?shut?down");});host.Run(); }點評區(qū)
說實話,相見恨晚,我在項目開發(fā)中也遇到了這種情況,我的場景是需要將 ?asp.net core 用 后臺服務 的方式承載,為了方便操作,我用了 topshelf ,官方網(wǎng)址:http://topshelf-project.com ?類似如下代碼:
public?class?TownCrier {readonly?Timer?_timer;public?TownCrier(){_timer?=?new?Timer(1000)?{AutoReset?=?true};_timer.Elapsed?+=?(sender,?eventArgs)?=>?Console.WriteLine("It?is?{0}?and?all?is?well",?DateTime.Now);}public?void?Start()?{?_timer.Start();?}public?void?Stop()?{?_timer.Stop();?} }public?class?Program {public?static?void?Main(){var?rc?=?HostFactory.Run(x?=>???????????????????????????????????//1{x.Service<TownCrier>(s?=>???????????????????????????????????//2{s.ConstructUsing(name=>?new?TownCrier());????????????????//3s.WhenStarted(tc?=>?tc.Start());?????????????????????????//4s.WhenStopped(tc?=>?tc.Stop());??????????????????????????//5});x.RunAsLocalSystem();???????????????????????????????????????//6x.SetDescription("Sample?Topshelf?Host");???????????????????//7x.SetDisplayName("Stuff");??????????????????????????????????//8x.SetServiceName("Stuff");??????????????????????????????????//9});?????????????????????????????????????????????????????????????//10var?exitCode?=?(int)?Convert.ChangeType(rc,?rc.GetTypeCode());??//11Environment.ExitCode?=?exitCode;} }現(xiàn)在看樣子不需要了。
總結
以上是生活随笔為你收集整理的如何实现 asp.net core 安全优雅退出 ?的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 一键生成Vue.js + Web API
 - 下一篇: 在业务层实现校验请求参数