简体   繁体   English

使用ASP.NET MVC Core运行后台任务

[英]Run a background task with asp.net mvc core

I am working on an asp.net mvc core 2 application. 我正在开发一个asp.net mvc core 2应用程序。

I want to run a kind of cron task in background on my application which launch something every 10 minutes. 我想在我的应用程序的后台运行一种cron任务,该任务每10分钟启动一次。 I have wrote a console application to do this but i am wondering if there is a way to put this code directly in asp.net mvc core project (Startup.cs ?) 我已经编写了一个控制台应用程序来执行此操作,但是我想知道是否有一种方法可以将此代码直接放入asp.net mvc核心项目(Startup.cs吗?)

It should be the same with the console application. 与控制台应用程序应该相同。 There is a main function in an asp.net core application. 在asp.net核心应用程序中有一个main功能。 Also, you can put it in StartUp.cs. 另外,您可以将其放在StartUp.cs中。

The only issue is if you are hosting your application in IIS for example. 唯一的问题是,例如,如果您将应用程序托管在IIS中。 Usually after sometimes without incoming request, your application will be put in to sleep mode (killed) and so does your background task. 通常,在有时没有传入请求之后,您的应用程序将进入睡眠模式(已终止),后台任务也是如此。 So you need to verify it carefully. 因此,您需要仔细验证。

Adding something to your Startup.cs is a way this could work. 将某些东西添加到您的Startup.cs中是一种可行的方法。

You can implement your job like that: 您可以像这样实现您的工作:

public class YourCronService: IHostedService
{

    public Task StartAsync(CancellationToken cancellationToken)
    {
        Task.Run(TaskRoutine, cancellationToken);
        return Task.CompletedTask;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        Console.WriteLine("Sync Task stopped");
        return null;
    }

    public Task TaskRoutine()
    {
        while (true)
        {
            //Do what ever you want to do all 10 Minutes

            //Wait 10 minutes till next execution
            DateTime nextStop = DateTime.Now.AddMinutes(10);
            var timeToWait = nextStop - DateTime.Now;
            var millisToWait = timeToWait.TotalMilliseconds;
            Thread.Sleep((int)millisToWait);
        }
    }
}

After implementing your service you just have to add the following Line of Code into your Startup.cs right into the ConfigureServices method: 实施服务后,只需将以下代码行添加到Startup.cs ,然后添加到ConfigureServices方法中:

services.AddSingleton<IHostedService, YourCronService>();

If your doing it this way you should defenitly check if your server is "always on". 如果以这种方式进行操作,则应该仔细检查服务器是否“始终处于打开状态”。 The job won't be executed if your server is in standby-mode. 如果您的服务器处于待机模式,则不会执行该作业。 An good example herefor would be an Auzure Web App. 一个很好的例子是Auzure Web App。 If your web-app isn't used in a while it will set into standby-mode until the next request unless you put the "always on"-feature on. 如果一段时间未使用您的网络应用程序,除非您启用“始终在线”功能,否则它将一直设置为待机模式直到下一个请求。

I have been able to do this in MVC Core with HangFire. 我已经可以在MVC Core中使用HangFire做到这一点。

Hangfire 吊火

I added Nuget Packages: Hangfire.AspNetCode, HangFire.Console and HangFire.SqlServer 我添加了Nuget程序包: Hangfire.AspNetCode,HangFire.Console和HangFire.SqlServer

Configure Services: 配置服务:

services.AddHangfire(x => x.UseSqlServerStorage(connectionForHangFire));

Configure: 配置:

app.UseHangfireDashboard();
app.UseHangfireServer();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM