问题描述
我正在使用Azure WebJob成功地从服务总线队列中获取消息.但是我想使用同一WebJob每5秒运行一些方法.
I'm using Azure WebJob to get messages from a service bus queue with success.But i wanted to use this same WebJob to run some methods every 5 seconds.
我尝试了以下方法,并且在本地运行良好,但是当我发布时,它只能运行一次.天蓝色日志没有错误.
I've tried the following approach, and locally it run fine, but when i publish it only runs once.No errors on azure logs.
我在做什么错了?
感谢您的帮助.
static void Main()
{
try
{
var testTimer = new System.Threading.Timer(e => TestMethod(), null, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(5));
SetupJobHost();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
private static void TestMethod()
{
Console.WriteLine("Test");
}
推荐答案
我建议采用其他方法,并使用 TimerTrigger .您可以使用一个简单的chron表达式,该表达式将使您的方法按设定的时间表执行.如果走这条路,请确保在调用之前将WebJob部署为触发的作业(不连续!),并在调用之前先调用 JobHostConfiguration
的 UseTimers()
方法 JobHost
的 RunAndBlock
方法.与滚动自己的计时器服务相比,这是一种更轻松,更干净的方法.
I recommend taking a different approach and using a TimerTrigger. You can use a simple chron expression that will cause your method to be executed on a set schedule. If you go this route, make sure that you deploy your WebJob as a triggered job (not continuous!) and that you call the JobHostConfiguration
's UseTimers()
method before calling the JobHost
's RunAndBlock
method. This is a much easier and cleaner approach than rolling your own timer service.
这篇关于带有队列和System.Threading.Timer的Azure WebJob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!