如何在ASP.Net Core中使用 IHostedService的方法
在我們應用程序中常常會有一些執(zhí)行后臺任務和任務調(diào)度的需求,那如何在 ASP.Net Core 中實現(xiàn)呢? 可以利用 Azure WebJobs 或者其他一些第三方任務調(diào)度框架,如:Quartz 和 Hangfire。
在 ASP.Net Core 中,也可以將 后臺任務 作為托管服務的模式,所謂的 托管服務 只需要實現(xiàn)框架中的 IHostedService 接口并囊括進你需要的業(yè)務邏輯作為后臺任務,這篇文章將會討論如何在 ASP.Net Core 中構建托管服務。
創(chuàng)建托管服務
要想創(chuàng)建托管服務,只需要實現(xiàn) IHostedService 接口即可,下面就是 IHostedService 接口的聲明。
public interface IHostedService
{
Task StartAsync(CancellationToken cancellationToken);
Task StopAsync(CancellationToken cancellationToken);
}
這一節(jié)中我們在 ASP.Net Core 中做一個極簡版的 托管服務, 首先自定義一個 MyFirstHostedService 托管類,代碼如下:
public class MyFirstHostedService : IHostedService
{
protected async override Task ExecuteAsync(CancellationToken token)
{
throw new NotImplementedException();
}
}
創(chuàng)建 BackgroundService
有一點要注意,上一節(jié)的 MyFirstHostedService 實現(xiàn)了 IHostedService 接口,實際開發(fā)中并不需要這樣做,因為 .Net Core 中已經(jīng)提供了抽象類 BackgroundService,所以接下來重寫抽象類的 ExecuteAsync 方法即可,如下代碼所示:
public class MyFirstHostedService : BackgroundService
{
protected async override Task ExecuteAsync(CancellationToken token)
{
throw new NotImplementedException();
}
}
下面的代碼片段展示了一個簡單的 Log 方法,用于記錄當前時間到文件中,這個方法由 托管服務 觸發(fā)。
private async Task Log()
{
using (StreamWriter sw = new StreamWriter(@"D:\log.txt",true))
{
await sw.WriteLineAsync(DateTime.Now.ToLongTimeString());
}
}
使用 ExecuteAsync 方法
接下來看看如何實現(xiàn) ExecuteAsync 方法,這個方法的邏輯就是周期性(second/s)的調(diào)用 Log() 方法,如下代碼所示:
protected async override Task ExecuteAsync(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
await Log();
await Task.Delay(1000, token);
}
}
好了,下面是完整的 MyFirstHostedService 類代碼,僅供參考。
using Microsoft.Extensions.Hosting;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace HostedServicesApp
{
public class MyFirstHostedService : BackgroundService
{
protected async override Task ExecuteAsync(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
await Log();
await Task.Delay(1000, token);
}
}
private async Task Log()
{
using (StreamWriter sw = new StreamWriter(@"D:\log.txt",true))
{
await sw.WriteLineAsync(DateTime.Now.ToLongTimeString());
}
}
}
}
托管服務注冊
托管服務類已經(jīng)寫好了,要想注入到 Asp.NET Core 中,需要在 Startup.ConfigureServices 中將 托管服務類 注入到 ServiceCollection 中,如下代碼所示:
public void ConfigureServices(IServiceCollection services)
{
services.AddHostedService<MyFirstHostedService>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
當把應用程序跑起來后,你會看見程序每秒都會往 D:\log.txt 文件中記錄日志。
在 IHostedService 中提供的 StartAsync 和 StopAsync 可用于在 ASP.NET Core 中執(zhí)行或停止后臺任務,你可以用它在你的應用程序中更新數(shù)據(jù)或其他操作,還有這些周期性業(yè)務邏輯是跑在后臺線程中的,這樣就不會導致主請求線程的阻塞。
譯文鏈接:https://www.infoworld.com/article/3390741/how-to-use-ihostedservice-in-aspnet-core.html
到此這篇關于如何在ASP.Net Core中使用 IHostedService的方法的文章就介紹到這了,更多相關ASP.Net Core使用 IHostedService內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
net core下鏈路追蹤skywalking安裝和簡單使用教程
本文將從0開始搭建兩個webapi項目,使用Skywalking來追蹤他們之間的調(diào)用關系及響應時間,開發(fā)環(huán)境為VisualStudio2019,對net core 鏈路追蹤skywalking安裝和使用教程感興趣的朋友一起看看吧2021-10-10
CheckBoxList兩列并排編譯為表格顯示具體實現(xiàn)
CheckBoxList兩列并排的顯示效果相比大家都有見到過吧,下面是具體的實現(xiàn)代碼,感興趣的朋友可以參考下哈2013-05-05
asp.net中利用ashx實現(xiàn)圖片防盜鏈的原理分析
盜鏈的危害我就不說了,網(wǎng)上有很多。下面是asp.net下利用ashx的防盜鏈原理分析2008-09-09

