【问题标题】:How do I keep an Azure WebJob from exiting without using JostHost or Azure Storage Account?如何在不使用 JostHost 或 Azure 存储帐户的情况下防止 Azure WebJob 退出?
【发布时间】:2017-07-13 18:29:47
【问题描述】:

我正在设置一个 Azure WebJob,它会持续为我做一些事情,但我无法阻止它退出。

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

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

如您所见,我目前正在尝试使用 JobHost.RunAndBlock() 方法来阻止我的工作退出。假设“一些其他代码/工作”立即将控制权返回给调用者,但确实需要应用程序稍后继续运行。

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

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

【问题讨论】:

    标签: c# azure azure-webjobs


    【解决方案1】:

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

    static void Main(string[] args)
    {   
       //Some other code/work
    
        while (true)
        {
            Task.Delay(1000).Wait();
            //or
            Thread.Sleep(1000);
        }
    }
    

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

    进一步的改进可能是检查是否需要取消,如果需要,则优雅地退出循环。

    【讨论】:

      猜你喜欢
      • 2019-02-01
      • 2021-12-21
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 2020-07-17
      • 2019-09-28
      • 1970-01-01
      • 2020-08-23
      相关资源
      最近更新 更多