经过大量挖掘,我意识到这可以通过 SDK 扩展类来完成TimerSchedule
.
为此,您需要一个能够用于多个触发器的基类。
class CustomTimerTriggerBase: TimerSchedule
{
TimeSpan timer;
public CustomTimerTriggerBase(string triggerConfigKey)
{
timer=TimeSpan.Parse(ConfigurationManager.AppSettings[triggerConfigKey]);
}
public override DateTime GetNextOccurrence(DateTime now)
{
return now.Add(timer);
}
}
使用这个基地来生成你的计时器......
public sealed class FooTimer : CustomTimerTriggerBase
{
public FooTimer() : base("FooTimerKey") {}
}
在你的 App.config 中有一个“FooTimer”键
<add key="FooTimerKey" value="00:02:00" />
在您的 webjob 函数中使用此 FooTimer 类。
public void foo([TimerTrigger(typeof(FooTimer)] TimerInfo timer)
现在,您只需更改应用程序配置中的值即可,而无需重新部署代码。NOTE:由于您使用 Timespan 进行解析,因此该字符串可以是 TimeSpan 格式中定义的任何您需要的格式。
UPDATE
正如所指出的我--''''''---------'''''''''''' https://stackoverflow.com/users/117700/l and 安迪·多贝多 https://stackoverflow.com/users/9032853/andy-dobedoe现在(截至 2019 年)实现这一目标要简单得多。
public static async Task RunAsync([TimerTrigger("%MYCRON%")]TimerInfo myTimer
找到名为 MYCRON 的设置并使用其中的 cron 表达式