我想创建一个 Web 作业(监听器)来监听存储中的所有队列。如果有任何新消息,则会触发处理程序。
Azure WebJobs SDK 提供了一种仅监听一个队列的解决方案:
public class Functions
{
// This function will get triggered/executed when a new message is written
// on an Azure Queue called queue.
public static async Task ProcessQueueMessage(
[QueueTrigger("%test%")] CloudQueueMessage message,
IBinder binder)
{
//do some stuff
}
}
这种方法很好,但我需要: 1)监听不同的队列 2)向这个类注入(inject)一个我认为我不能的类
所以我正在考虑创建自己的监听器。我想创建多个威胁,每个威胁都会监听一个队列。然后,当我运行网络作业时,它会启动所有威胁。
我想知道是否有人可以提出更好的解决方案。如果有代码示例就太好了。
谢谢
最佳答案
除非您确实想要,否则不需要创建自己的监听器。 Azure Webjobs SDK 已经为您完成了繁重的工作。
这是一个示例 Functions.cs,它可以处理来自不同队列的数据。 如果需要,您可以将服务注入(inject)到 Functions.cs 中,以便不同的队列由不同的服务处理。
private readonly IMyService _myService;
//You can use Dependency Injection if you want to.
public Functions(IMyService myService)
{
_myService = myService;
}
public void ProcessQueue1([QueueTrigger("queue1")] string item)
{
//You can get the message as a string or it can be strongly typed
_myService.ProcessQueueItem1(item);
}
public void ProcessQueue2([QueueTrigger("queue2")] MyObject item)
{
//You can get the message as a string or it can be strongly typed
_myService.ProcessQueueItem2(item);
}
希望这有帮助
关于c# - 多个 azure 队列的一个监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46964689/