在本地运行计时器触发的 Azure 函数一次的最简单方法是什么? [英] What is the simplest way to run a timer-triggered Azure Function locally once?

查看:20
本文介绍了在本地运行计时器触发的 Azure 函数一次的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个使用 定时器触发器.我已经像这样设置了它们,其中 %TimerSchedule% 指的是应用程序设置中的 cron 表达式:

I have a few C# Azure Functions that run on a schedule using timer triggers. I've set them up like so, where %TimerSchedule% refers to a cron expression in the app settings:

public static void Run([TimerTrigger("%TimerSchedule%")]TimerInfo myTimer, TraceWriter log)

在开发过程中,我经常想使用 Azure Functions Tools for Visual Studio + Azure Functions Core Tools 在本地运行函数.但是当我按 F5 在本地调试该函数时,它(通常)不会立即运行.相反,它会根据计时器计划开始等待下一次发生.例如,如果我的 cron 表达式说每天晚上 8 点运行,我必须等到晚上 8 点才能在我的机器上实际运行该函数.

During development, I often want to run the functions locally using Azure Functions Tools for Visual Studio + Azure Functions Core Tools. But when I hit F5 to debug the function locally it (usually) doesn't run immediately. Instead, it starts waiting for the next occurrence as per the timer schedule. So for example, if my cron expression says to run daily at 8PM, I'd have to wait until 8PM for the function to actually run on my machine.

所以我的问题是:让函数在本地运行一次的最简单和最好的方法是什么?

我尝试过或考虑过的事情:

Things I have tried or considered:

  1. 为本地开发使用更频繁的计时器计划
    • 这没问题,但并不完美——除非非常频繁,否则您仍然需要等待一段时间,如果非常频繁,则该函数可能会运行多次.这就是我现在正在做的事情.
  • 这不是 100% 简单,因为您必须向 Run() 提供 TimerInfoTraceWriter 参数——我发现令人惊讶的是,这方面的文档很少.
  • This isn't 100% straightforward because you have to provide TimerInfo and TraceWriter arguments to Run() – and I've found surprisingly little documentation for that.

Microsoft 的 在 Azure 中测试代码的策略函数页面对这个主题的帮助不大——它只提到定时器触发器是测试其他触发器类型的一种方式.

Microsoft's Strategies for testing your code in Azure Functions page is not very helpful on this topic – it only mentions timer triggers as a way to test other trigger types.

在理想情况下,我按下 F5 后该函数会立即运行一次——就像开发一个普通"的 .NET 应用程序一样.

In a perfect world, I'd hit F5 and the function would immediately run once – just like developing a "normal" .NET app.

推荐答案

我有同样的问题,并使用 DEBUG 标志仅在调试时让 RunOnStartup :

I had the same question, and used the DEBUG-flag to have the RunOnStartup only while debugging:

        public static void Run(
            [TimerTrigger("* 0 7 * * 1-5"
#if DEBUG
            , RunOnStartup=true
#endif
            )]TimerInfo myTimer, TraceWriter log)
        {

这篇关于在本地运行计时器触发的 Azure 函数一次的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆