如何在不使用 JostHost 或 Azure 存储帐户的情况下防止 Azure WebJob 退出?

How do I keep an Azure WebJob from exiting without using JostHost or Azure Storage Account?

我正在设置一个 Azure WebJob,它不断为我做事,但我无法阻止它退出。

static void Main(string[] args)
{   
   //Some other code/work

   JobHostConfiguration config = new JobHostConfiguration();
   config.DashboardConnectionString = null;
   new JobHost(config).RunAndBlock();       
}

如您所见,我目前正在尝试使用 JobHost.RunAndBlock() 方法来防止我的作业退出。假设 "Some other code/work" returns 立即控制调用者,但确实要求应用程序稍后保留 运行。

使用 JobHost 需要我设置一个 Azure 存储帐户,因为它要使用 AzureWebJobsStorage 连接字符串记录内容。但是我不想使用此功能来记录我的东西,我只是希望我的应用程序保留 运行 而我不想创建 Azure 存储帐户。

那么,如果我不想创建 Azure 存储帐户或 JobHost,我可以使用什么代码来防止我的代码退出?

在没有 JobHost 的情况下保持 WebJob 活动的最简单方法(也是一个非常简单的示例)是在其中有一个连续循环:

static void Main(string[] args)
{   
   //Some other code/work

    while (true)
    {
        Task.Delay(1000).Wait();
        //or
        Thread.Sleep(1000);
    }
}

根据应该触发该功能的内容,您可以检查时间/消息/要完成的工作/while 循环内的任何内容,并在必要时触发工作。

进一步的改进可能是检查是否需要取消并在需要时优雅地退出循环。