Azure WebJob 最佳方法

Azure WebJob Best Approach

这是我第一次做WebJob类型的应用。我创建了一个 webjob 项目,在解决方案中它带有 Program.csFunction.cs.

我已经删除了 Function.cs 因为在这个项目中没有我将从中获取数据的队列。

现在在Program.cs中已经有如下的Main Method:

class Program
{
    // Please set the following connection strings in app.config for this WebJob to run:
    // AzureWebJobsDashboard and AzureWebJobsStorage
    static void Main()
    {
        var host = new JobHost();
        // The following code ensures that the WebJob will be running continuously
        host.RunAndBlock();
    }
}

据我了解,RunAndBlock 是连续 运行 网络作业,但我希望作业只 运行 一次。我想通过时间表从外部控制执行。我想知道如何让我的代码 运行 只执行一次? 如下所示,我有一个 SupportService Class 有 RunOnePoolProvisioingCycle,我只想调用此方法一次。 这是正确的方法吗?

static void Main()
{
    SupportService _supportService = new SupportService();
    _supportService.Initialize();
    _supportService.SetPoolProvisioningConfigurations();
    _supportService.RunOnePoolProvisioningCycle();
}

还是这个?

static void Main()
{
    var host = new JobHost();
    SupportService _supportService = new SupportService();
    _supportService.Initialize();
    _supportService.SetPoolProvisioningConfigurations();
    host.Call(typeof(SupportService).GetMethod("SetPoolProvisioningConfigurations"));
}

还是这个?

static void Main()
{
    var host = new JobHost();
    SupportService _supportService = new SupportService();
    _supportService.Initialize();
    _supportService.SetPoolProvisioningConfigurations();
    host.CallAsync(typeof(SupportService).GetMethod("SetPoolProvisioningConfigurations"));
}

或者我应该使用:

host.Start()

host.StartAsync()?

您看到的是SDK的一部分,是可选的。 Web 作业可以像控制台应用程序一样简单,您可以按原样压缩、上传和 运行。

所以这段代码似乎是您的最佳选择:

static void Main()
{
    SupportService _supportService = new SupportService();
    _supportService.Initialize();
    _supportService.SetPoolProvisioningConfigurations();
    _supportService.RunOnePoolProvisioningCycle();
}

模板创建的 WebJob 使用 WebJobs SDK. If you don't need to use any of the features of the SDK, then you can just create a console app and set up a CRON schedule to run it on (see "Scheduled jobs" here).

我链接到上面有关 WebJobs SDK 的更多信息。除了促进您想要在 queues/blobs/etc 上触发功能的场景外,它还能够通过 TimerTrigger 按计划 运行 您的作业(部分SDK extensions)。阅读这些材料,看看哪个最适合您的需求。