带有队列和System.Threading.Timer的Azure WebJob [英] Azure WebJob with queues and System.Threading.Timer
问题描述
我正在使用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");
}
推荐答案
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!