ASP.NET Core MVC planned task

I need to run a scheduled task every new day in the main asp.net mvc application. Can I do it and how ?!

Thnx

+8
source share
6 answers

.

, Quarz, Hangfire Azure WebJobs, ASP.NET Core.

, Quarz Hangfire ASP.NET Core, , , IIS Azure , , IIS (- - ), (IIS , - ).

, , IIS - . , ASP.NET Core ( , Azure Azure Web Jobs).

+7

Startup.cs System.Threading.Timer.

// add the below into your Startup contructor
public Startup(IHostingEnvironment env)
{
    // [...]
    var t = new System.Threading.Timer(doSomething);
    // do something every 15 seconds
    t.Change(0, 15000);
}

// define a DateTime property to hold the last executed date
public DateTime LastExecuted { get; set; }

// define the doSomething callback in your Startup class
private void doSomething(object state)
{
    System.Diagnostics.Debug.WriteLine("The timer callback executes.");

    // this callback is called every 15 seconds
    // but the condition below makes sure only it
    // only takes place at 17:00 
    // every day, when LastExecuted is not today
    if(DateTime.UtcNow.Hour == 17 && DateTime.UtcNow.Minute == 0 && DateTime.UtcNow.Date != LastExecuted.Date)
    {
        LastExecuted = DateTime.UtcNow;
        System.Diagnostics.Debug.WriteLine("#### Do the job");
    }
}
+3

powershell script, GET api/endpoint . , Windows .NET Core Application.

1. powershell script myjob.ps1

$url="http://www.myapp.com/api/runjob"
$content=(New-Object System.Net.WebClient).DownloadString("$url");

$Logfile = "D:\Temp\job.log"
Add-content $Logfile -value $content

2. powershell

Powershell :

Set-Executionpolicy -Scope CurrentUser -ExecutionPolicy UnRestricted
Set-Executionpolicy -Scope Process -ExecutionPolicy UnRestricted

: https://www.metalogix.com/help/Content%20Matrix%20Console/SharePoint%20Edition/002_HowTo/004_SharePointActions/012_SchedulingPowerShell.htm

3. script

Task Schedulerer. " "

Program: Powershell
Argument: -noprofile -executionpolicy bypass -file "C:\Jobs\myjob.ps1"
+2

Azure, . Azure WebJobs URL-, , cronjob, , . asp.net, . , VS .

For example, let it listen in on a queue and send an email when something is queued. Or create a thumbnail of a large image.

Mor info can be found here: https://azure.microsoft.com/en-us/documentation/articles/websites-dotnet-webjobs-sdk-get-started/

+1
source

Use the features of Azure. That's what it was built for. This is pretty cool.

https://docs.microsoft.com/en-us/azure/azure-functions/functions-overview

0
source

Source: https://habr.com/ru/post/1657809/


All Articles