What is the simplest way to run a timer-triggered Azure Function locally once?(在本地运行一次计时器触发的 Azure 函数的最简单方法是什么?)
问题描述
我有一些 C# Azure Functions 使用 定时器触发器.我已经将它们设置为这样,其中 %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:
- 仅针对本地开发使用更频繁的计时器时间表
- 这没问题,但并不完美——除非非常频繁,否则您仍然需要稍等片刻,如果非常频繁,则该函数可能会运行多次.这就是我现在正在做的事情.
- 这不是 100% 简单的,因为您必须向
Run()
提供TimerInfo
和TraceWriter
参数——我发现令人惊讶的是,这方面的文档很少.
- This isn't 100% straightforward because you have to provide
TimerInfo
andTraceWriter
arguments toRun()
– 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-flag 仅在调试时拥有 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 函数的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在本地运行一次计时器触发的 Azure 函数的最简单方法是什么?


- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- C# 中多线程网络服务器的模式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01