C# 在Azure Webjobs SDK v3的ServiceBusTrigger函数中处理Webjob关闭

C# 在Azure Webjobs SDK v3的ServiceBusTrigger函数中处理Webjob关闭,c#,azure-webjobs,C#,Azure Webjobs,我试图在我的函数中优雅地处理webjob关闭。我正在使用Azure Webjobs SDK的v3和服务总线扩展 下面是我根据一些示例代码编写的测试函数: 但是,当我关闭webjob时,取消操作不会被记录下来 我还尝试捕获TaskCanceledException,如本例所示: 那对我也不管用。如何在我的功能中实现这一点 更新(2018年12月18日): 虽然我还没有弄明白这一点,但我有一个适合我的目的的解决办法。在我的程序类中,我声明了一个public static CancellationTo

我试图在我的函数中优雅地处理webjob关闭。我正在使用Azure Webjobs SDK的v3和服务总线扩展

下面是我根据一些示例代码编写的测试函数:

但是,当我关闭webjob时,取消操作不会被记录下来

我还尝试捕获TaskCanceledException,如本例所示:

那对我也不管用。如何在我的功能中实现这一点

更新(2018年12月18日):

虽然我还没有弄明白这一点,但我有一个适合我的目的的解决办法。在我的程序类中,我声明了一个
public static CancellationToken shutdownToken
变量,并在Main方法中将其设置为

shutdownToken = new WebJobsShutdownWatcher().Token;
然后我在我的函数中注册一个回调,如下所示:

Program.shutdownToken.Register(() => logWriter.LogWarning("Webjob is shutting down!"));

我参考您的链接代码,用
QueueTrigger
编写一个
WebJob
,然后上传并在运行一段时间后停止它。我的输出日志显示它工作正常。也许你可以参考一下

public static string ShutDownFilePath
    {
        get
        {
            return shutDownFile;
        }
    }

    public static void ShutdownMonitorJob(
         [QueueTrigger("myqueue")] string message,
         TextWriter log,
         CancellationToken cancellationToken)
    {

        new Thread(new ThreadStart(() =>
        {
            log.WriteLine("From thread: In about 10 seconds, the job will be signaled to stop");
            Thread.Sleep(10000);

            // Modify the shutdown file
            File.WriteAllText(shutDownFile, string.Empty);
        })).Start();

        log.WriteLine("From function: Received a message: " + message);

        while (!cancellationToken.IsCancellationRequested)
        {
            log.WriteLine("From function: Cancelled: No");
            Thread.Sleep(2000);
        }

        // Perform the graceful shutdown logic here
        log.WriteLine("From function: Cancelled: Yes");
    }
这是我的输出日志。取消状态更改为是

[12/18/2018 02:14:07 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:07 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:07 > dd4ec8: INFO] info: Function[0]
[12/18/2018 02:14:07 > dd4ec8: INFO]       From function: Cancelled: No
[12/18/2018 02:14:07 > dd4ec8: INFO]       
[12/18/2018 02:14:09 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:09 > dd4ec8: INFO] From function: Cancelled: No
[12/18/2018 02:14:09 > dd4ec8: INFO] info: Function[0]
[12/18/2018 02:14:09 > dd4ec8: INFO]       From function: Cancelled: No
[12/18/2018 02:14:09 > dd4ec8: INFO]       
[12/18/2018 02:14:09 > dd4ec8: SYS INFO] Detected WebJob file/s were updated, 
refreshing 
WebJob
[12/18/2018 02:14:10 > dd4ec8: SYS INFO] Status changed to Stopping
[12/18/2018 02:14:11 > dd4ec8: INFO] From function: Cancelled: Yes
[12/18/2018 02:14:11 > dd4ec8: INFO] From function: Cancelled: Yes
[12/18/2018 02:14:11 > dd4ec8: INFO] info: Function[0]
[12/18/2018 02:14:11 > dd4ec8: INFO]       From function: Cancelled: Yes
[12/18/2018 02:14:11 > dd4ec8: INFO]       
[12/18/2018 02:14:11 > dd4ec8: INFO] Executed 'Functions.ShutdownMonitorJob' 
(Succeeded, Id=9ce21c43-c113-4794-b565-81cb615a29ab)
[12/18/2018 02:14:11 > dd4ec8: INFO] info: Host.Results[0]
[12/18/2018 02:14:11 > dd4ec8: INFO]       Executed 'Functions.ShutdownMonitorJob' 
(Succeeded, Id=9ce21c43-c113-4794-b565-81cb615a29ab)
[12/18/2018 02:14:11 > dd4ec8: INFO] Job host stopped
我用ServiceBus触发器做了更多的测试

这是我的Program.cs内容

static void Main(string[] args)
    {

        JobHostConfiguration config = new JobHostConfiguration();
        config.UseServiceBus();
        JobHost host = new JobHost(config);
        host.RunAndBlock();

    }
这是我的函数内容

private static string shutDownFile = Path.GetTempFileName();

    public static string ShutDownFilePath
    {
        get
        {
            return shutDownFile;
        }
    }


    public static void ShutdownMonitorJob(
         [ServiceBusTrigger("myqueue")]
         string myQueueItem,
         TextWriter log,
         CancellationToken cancellationToken)
    {

        new Thread(new ThreadStart(() =>
        {
            log.WriteLine("From thread: In about 10 seconds, the job will be signaled to stop");
            Thread.Sleep(10000);

            // Modify the shutdown file
            File.WriteAllText(shutDownFile, string.Empty);
        })).Start();

        log.WriteLine("From function: Received a message: " + myQueueItem);

        while (!cancellationToken.IsCancellationRequested)
        {
            log.WriteLine("From function: Cancelled: No");
            Thread.Sleep(2000);
        }

        // Perform the graceful shutdown logic here
        log.WriteLine("From function: Cancelled: Yes");
    }

}
这是我的新日志照片


我停止了
webjob
,状态正常更改。

我正在使用ServiceBustigger,但它对我不起作用。我意识到该示例是针对QueueTrigger的,但未能找到特定于ServiceBustigger的示例。@Brian Campbell我更新了答案,我测试了ServiceBus触发器。对我来说没问题。谢谢George。它对我仍然不起作用。我也把你的函数方法复制到了我的代码中。需要注意的几件事:我的webjob是一个.NET核心应用程序,我正在使用webjobs SDK的v3.0.3。作业托管模型与您使用的版本不同,因此这可能是问题的一部分。我将把这个作为Github问题提交给webjobs团队,看看他们是否有更多的信息。同时,我将继续使用我的变通方法。
private static string shutDownFile = Path.GetTempFileName();

    public static string ShutDownFilePath
    {
        get
        {
            return shutDownFile;
        }
    }


    public static void ShutdownMonitorJob(
         [ServiceBusTrigger("myqueue")]
         string myQueueItem,
         TextWriter log,
         CancellationToken cancellationToken)
    {

        new Thread(new ThreadStart(() =>
        {
            log.WriteLine("From thread: In about 10 seconds, the job will be signaled to stop");
            Thread.Sleep(10000);

            // Modify the shutdown file
            File.WriteAllText(shutDownFile, string.Empty);
        })).Start();

        log.WriteLine("From function: Received a message: " + myQueueItem);

        while (!cancellationToken.IsCancellationRequested)
        {
            log.WriteLine("From function: Cancelled: No");
            Thread.Sleep(2000);
        }

        // Perform the graceful shutdown logic here
        log.WriteLine("From function: Cancelled: Yes");
    }

}