Azure webjob关闭时更新存储表

Azure webjob关闭时更新存储表,azure,azure-webjobs,azure-webjobssdk,Azure,Azure Webjobs,Azure Webjobssdk,我的问题与下面的问题相似 我使用了来自的想法,但后来遇到了一个小障碍 我在webjob中设置了一个文件监视程序,如果webjob从门户关闭,它会被触发 在webjob终止之前,我需要更新存储表中的一些标志 问题是,我的代码似乎在试图从存储表中检索记录时停止。我在下面的代码中有异常处理程序,控制台上没有写异常消息 下面是我的代码 CloudStorageAccount storageAccount = CloudStorageAccount.Parse("my storage key"); va

我的问题与下面的问题相似

我使用了来自的想法,但后来遇到了一个小障碍

我在webjob中设置了一个文件监视程序,如果webjob从门户关闭,它会被触发

在webjob终止之前,我需要更新存储表中的一些标志

问题是,我的代码似乎在试图从存储表中检索记录时停止。我在下面的代码中有异常处理程序,控制台上没有写异常消息

下面是我的代码

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("my storage key");
var tableClient = storageAccount.CreateCloudTableClient();
var table = tableClient.GetTableReference("myTable");
TableOperation operation = TableOperation.Retrieve("partKey", "rowKey");
var result = table.Execute(operation); // stucks here
   if (result.Result != null)
     {
        MyEntity entity = (MyEntity)result.Result;
        if (entity != null)
         {
           entity.IsRunning = false; //reset the flag
           TableOperation update = TableOperation.InsertOrReplace(entity);
           table.Execute(update); //update the record
         }
     }
我已将
设置中的
停止等待时间
增加到300秒,但仍然没有运气。

您可以使用
这是Amit解决方案的一个实现:

因此,我找到了一个解决方案:
程序中没有修改。cs

class Program
{
    static void Main()
    {
        var host = new JobHost();
        host.Call(typeof(Startup).GetMethod("Start"));
        host.RunAndBlock();
    }
}
在您的功能中会出现优雅的关机:

public class Startup
{
    [NoAutomaticTrigger]
    public static void Start(TextWriter log)
    {
        var token = new Microsoft.Azure.WebJobs.WebJobsShutdownWatcher().Token;
        //Shut down gracefully
        while (!token.IsCancellationRequested)
        {
            // Do somethings
        }

       // This code will be executed once the webjob is going to shutdown
       Console.Out.WriteLine("Webjob is shuting down")
    }
}
在while循环之后,您还可以停止已启动的任务